import * as ReactDOM from "react-dom";
import { Buffer } from "buffer";
import { jsxDEV } from "react/jsx-dev-runtime";
import { ErrorBus, DevErrorManager } from "../components/Dev/DevErrorManager.js";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/bootstrap/errorHandling.tsx";
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 = (renderer) => {
  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);
      }
    }
  };
  renderer(getErrorContainer(), /* @__PURE__ */ jsxDEV(DevErrorManager, {
    bus: errorBus
  }, void 0, false, {
    fileName: _jsxFileName,
    lineNumber: 45,
    columnNumber: 32
  }, void 0), () => {
  });
  return async (cb) => {
    try {
      await cb((e) => errorBus.handleError("Recoverable React error", e));
    } catch (e) {
      errorBus.handleError("Entrypoint crash", e);
    }
  };
};
const createErrorHandler = (renderer) => {
  {
    return devErrorHandler(renderer);
  }
};
export {
  createErrorHandler
};
//# sourceMappingURL=errorHandling.js.map
