"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const ui = require("@contember/ui");
const react = require("react");
const useParsedStacktrace = require("./useParsedStacktrace.cjs");
class ErrorBus {
  constructor() {
    __publicField(this, "queue", []);
    __publicField(this, "listener", null);
  }
  async handleError(source, error) {
    const boxedError = { error, source };
    if (!this.listener) {
      this.queue.push(boxedError);
    } else {
      this.listener(boxedError);
    }
  }
  register(listener) {
    this.listener = listener;
    this.queue.forEach(listener);
    this.queue = [];
    return () => {
      this.listener = null;
    };
  }
}
function DevErrorManager(props) {
  const [errors, setErrors] = react.useState([]);
  const [errIndex, setErrorIndex] = react.useState(0);
  const [open, setOpen] = react.useState(false);
  react.useEffect(
    () => {
      return props.bus.register((err) => {
        setTimeout(
          () => {
            setErrors((errors2) => [err, ...errors2]);
            setOpen(true);
          },
          0
        );
      });
    },
    [props.bus]
  );
  const currentError = errors[errIndex];
  const processedError = useParsedStacktrace.useProcessedError(currentError?.error);
  if (!currentError) {
    return null;
  }
  const onPrevious = () => setErrorIndex((it) => Math.max(0, it - 1));
  const onNext = () => setErrorIndex((it) => Math.min(errors.length - 1, it + 1));
  const onClose = () => setOpen(false);
  if (!open) {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.DevErrorBadge, { errorCount: errors.length, onOpen: () => setOpen(true) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/DevErrorManager.tsx",
      lineNumber: 63,
      columnNumber: 10
    }, this);
  }
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    ui.DevErrorList,
    {
      currentError: processedError,
      currentErrorSource: currentError.source,
      currentErrorIndex: errIndex,
      errorCount: errors.length,
      onPrevious,
      onNext,
      onClose
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/Dev/DevErrorManager.tsx",
      lineNumber: 66,
      columnNumber: 3
    },
    this
  );
}
exports.DevErrorManager = DevErrorManager;
exports.ErrorBus = ErrorBus;
//# sourceMappingURL=DevErrorManager.cjs.map
