import f from "../../../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import * as e from "react";
import C from "../../_util/hooks/usePatchElement.mjs";
import { withInfo as R, withSuccess as g, withError as k, withWarn as _, withConfirm as A } from "../confirm.mjs";
import F from "../destroyFns.mjs";
import Q from "./HookModal.mjs";
let v = 0;
const H = /* @__PURE__ */ e.memo(/* @__PURE__ */ e.forwardRef((u, c) => {
  const [s, n] = C();
  return e.useImperativeHandle(c, () => ({
    patchElement: n
  }), []), /* @__PURE__ */ e.createElement(e.Fragment, null, s);
}));
function $() {
  const u = e.useRef(null), [c, s] = e.useState([]);
  e.useEffect(() => {
    c.length && (f(c).forEach((d) => {
      d();
    }), s([]));
  }, [c]);
  const n = e.useCallback((m) => function(E) {
    var a;
    v += 1;
    const i = /* @__PURE__ */ e.createRef();
    let p;
    const w = new Promise((t) => {
      p = t;
    });
    let h = !1, r;
    const y = /* @__PURE__ */ e.createElement(Q, {
      key: `modal-${v}`,
      config: m(E),
      ref: i,
      afterClose: () => {
        r == null || r();
      },
      isSilent: () => h,
      onConfirm: (t) => {
        p(t);
      }
    });
    return r = (a = u.current) === null || a === void 0 ? void 0 : a.patchElement(y), r && F.push(r), {
      destroy: () => {
        function t() {
          var o;
          (o = i.current) === null || o === void 0 || o.destroy();
        }
        i.current ? t() : s((o) => [].concat(f(o), [t]));
      },
      update: (t) => {
        function o() {
          var l;
          (l = i.current) === null || l === void 0 || l.update(t);
        }
        i.current ? o() : s((l) => [].concat(f(l), [o]));
      },
      then: (t) => (h = !0, w.then(t))
    };
  }, []);
  return [e.useMemo(() => ({
    info: n(R),
    success: n(g),
    error: n(k),
    warning: n(_),
    confirm: n(A)
  }), []), /* @__PURE__ */ e.createElement(H, {
    key: "modal-holder",
    ref: u
  })];
}
export {
  $ as default
};
