import { defineComponent as m, h as P, getCurrentInstance as C, ref as O, toRef as S, openBlock as u, createElementBlock as v, Fragment as p, renderSlot as c, renderList as $, unref as V, createBlock as f, withCtx as h, resolveDynamicComponent as x, normalizeProps as z, guardReactiveProps as E, inject as j, provide as F, onUnmounted as N, reactive as Q, computed as g, toRaw as U, createVNode as W, useSlots as X } from "vue";
const _ = Symbol("ModalStateToken"), L = Symbol("ModalIdToken"), M = () => j(_, {}), q = () => j(L, ""), R = /* @__PURE__ */ m({
  __name: "CreateModal",
  props: {
    modalId: {},
    provides: {}
  },
  setup(s) {
    const t = C(), { action: r } = M(), o = s;
    return F(L, o.modalId), o.provides && Object.assign(t == null ? void 0 : t.provides, o.provides), N(() => {
      o.modalId && (r == null || r.remove(o.modalId));
    }), (a, e) => c(a.$slots, "default");
  }
});
function A() {
  let s = 0;
  const t = () => `__modal_${s++}`, r = Symbol("ModalId"), o = Q({}), a = {};
  return F(_, {
    store: o,
    action: {
      show: (e, n) => {
        n && (o[e].args = {
          ...o[e].args,
          ...n
        }), o[e].visible = !0;
      },
      hide: (e) => {
        o[e].visible = !1;
      },
      remove: (e) => {
        delete o[e], delete a[e];
      },
      getModalId: (e) => (e[r] || (e[r] = t()), e[r]),
      register: (e, n, l, d) => {
        o[e] ? o[e].args = l : o[e] = {
          comp: n,
          visible: !1,
          args: l,
          provide: d
        };
      }
    },
    modalPromise: a
  }), {
    modalList: g(() => Object.keys(o).filter((e) => !!o[e]).filter((e) => o[e]).map((e) => ({
      id: e,
      ...o[e],
      comp: U(o[e].comp)
    })))
  };
}
function H() {
  const {
    modalList: s
  } = A();
  return {
    ModalContent: /* @__PURE__ */ m({
      name: "ModalContent",
      render() {
        return s.value.map((t) => P(R, {
          modalId: t.id,
          provides: t.provide
        }, () => P(t.comp, t.args)));
      }
    })
  };
}
function J(s, t) {
  var r, o;
  const a = C(), e = a.provides[_];
  let n = M();
  e && (n = e);
  const l = (r = n.action) == null ? void 0 : r.getModalId(s), d = O(!0), w = (o = n.store) == null ? void 0 : o[l];
  return {
    show: (i) => {
      var b, k;
      if ((d.value || !w) && ((b = n.action) == null || b.register(
        l,
        s,
        t,
        //@ts-ignore
        a == null ? void 0 : a.provides
      ), d.value = !1), !n.modalPromise[l]) {
        let y, I;
        const K = new Promise((T, D) => {
          y = T, I = D;
        });
        n.modalPromise[l] = {
          promise: K,
          reject: I,
          resolve: y
        };
      }
      return (k = n.action) == null || k.show(l, i), n.modalPromise[l].promise;
    },
    hide: () => {
      var i;
      return (i = n.action) == null ? void 0 : i.hide(l);
    }
  };
}
function Y() {
  const {
    store: s,
    action: t,
    modalPromise: r
  } = M(), o = q(), a = s[o], e = (i) => t == null ? void 0 : t.show(o, i), n = () => t == null ? void 0 : t.hide(o), l = () => t == null ? void 0 : t.remove(o), d = (i) => r[o].resolve(i), w = (i) => r[o].reject(i);
  return {
    show: e,
    hide: n,
    visible: S(a, "visible"),
    args: S(a, "args"),
    remove: l,
    resolve: d,
    reject: w
  };
}
const Z = /* @__PURE__ */ m({
  __name: "ModalProvider",
  setup(s) {
    const { modalList: t } = A();
    return (r, o) => (u(), v(
      p,
      null,
      [
        c(r.$slots, "default"),
        (u(!0), v(
          p,
          null,
          $(V(t), (a) => (u(), f(R, {
            key: a.id,
            provides: a.provide,
            "modal-id": a.id
          }, {
            default: h(() => [
              (u(), f(
                x(a.comp),
                z(E(a.args)),
                null,
                16
                /* FULL_PROPS */
              ))
            ]),
            _: 2
            /* DYNAMIC */
          }, 1032, ["provides", "modal-id"]))),
          128
          /* KEYED_FRAGMENT */
        ))
      ],
      64
      /* STABLE_FRAGMENT */
    ));
  }
}), B = /* @__PURE__ */ m({
  name: "If",
  __name: "If",
  props: {
    when: {}
  },
  setup(s) {
    const t = (r) => r;
    return (r, o) => r.when ? c(r.$slots, "default", {
      key: 0,
      value: t(r.when)
    }) : c(r.$slots, "fallback", { key: 1 });
  }
}), ee = /* @__PURE__ */ m({
  name: "For",
  inheritAttrs: !1,
  __name: "For",
  props: {
    echo: {},
    rowKey: {}
  },
  setup(s) {
    const t = s, r = g(() => t.echo);
    return (o, a) => (u(), v(
      p,
      null,
      [
        (u(!0), v(
          p,
          null,
          $(o.echo, (e, n) => c(o.$slots, "default", {
            key: o.rowKey,
            item: e,
            index: n
          })),
          128
          /* KEYED_FRAGMENT */
        )),
        W(B, {
          when: !r.value || !r.value.length || r.value.length === 0
        }, {
          default: h(() => [
            c(o.$slots, "fallback")
          ]),
          _: 3
          /* FORWARDED */
        }, 8, ["when"])
      ],
      64
      /* STABLE_FRAGMENT */
    ));
  }
}), oe = /* @__PURE__ */ m({
  name: "Switch",
  __name: "Switch",
  setup(s) {
    const t = X();
    function r() {
      return t.default ? t.default() : null;
    }
    function o(e, n) {
      Array.isArray(e) || (e = [e]);
      for (let l = 0; l < e.length; l++) {
        const d = e[l].props && e[l].props.when;
        if (d)
          return [l, d, e[l]];
      }
      return [-1];
    }
    const a = g(() => {
      let e = r();
      if (!e)
        return;
      const [n, l, d] = o(e);
      return {
        index: n,
        when: l,
        slot: d
      };
    });
    return (e, n) => {
      var l;
      return u(), f(B, {
        when: ((l = a.value) == null ? void 0 : l.index) !== -1
      }, {
        fallback: h(() => [
          c(e.$slots, "fallback")
        ]),
        default: h(() => {
          var d;
          return [
            (u(), f(x((d = a.value) == null ? void 0 : d.slot)))
          ];
        }),
        _: 3
        /* FORWARDED */
      }, 8, ["when"]);
    };
  }
}), re = /* @__PURE__ */ m({
  name: "Match",
  __name: "Match",
  props: {
    when: {}
  },
  setup(s) {
    const t = (r) => r;
    return (r, o) => c(r.$slots, "default", {
      value: t(r.when)
    });
  }
});
export {
  ee as For,
  B as If,
  re as Match,
  Z as ModalProvider,
  oe as Switch,
  H as createdModalContext,
  J as useModal,
  Y as useModalRef
};
//# sourceMappingURL=external.mjs.map
