import { createElement } from "react";
import * as ReactDOM from "react-dom";
import { Buffer } from "buffer";
import { ErrorBus, DevErrorManager } from "../components/Dev/DevErrorManager.js";
const getErrorContainer = () => {
  const errorElementId = "__contember__dev__error__container__element";
  let errorContainer = document.getElementById(errorElementId);
  if (errorContainer) {
    ReactDOM.unmountComponentAtNode(errorContainer);
  } else {
    errorContainer = document.createElement("div");
    errorContainer.id = errorElementId;
    document.body.appendChild(errorContainer);
  }
  return errorContainer;
};
const devErrorHandler = () => {
  const errorBus = new ErrorBus();
  window.Buffer = Buffer;
  window.addEventListener("error", (e) => {
    if (e.message.startsWith("ResizeObserver")) {
      return;
    }
    errorBus.handleError("Unhandled error", e.error);
  });
  window.addEventListener("unhandledrejection", (e) => errorBus.handleError("Unhandled promise rejection", e.reason));
  const origOnError = console.error;
  console.error = (...args) => {
    origOnError(...args);
    for (const arg of args) {
      if (arg instanceof Error) {
        errorBus.handleError("Logged error", arg);
      }
    }
  };
  ReactDOM.render(/* @__PURE__ */ createElement(DevErrorManager, {
    bus: errorBus
  }), getErrorContainer());
  return async (cb) => {
    try {
      await cb();
    } catch (e) {
      errorBus.handleError("Entrypoint crash", e);
    }
  };
};
const createErrorHandler = () => {
  {
    return devErrorHandler();
  }
};
export { createErrorHandler };
//# sourceMappingURL=errorHandling.js.map
