import { jsx as b } from "react/jsx-runtime";
import { OverlayStackingService as k } from "./index-OverlayStackingService.js";
import { useRef as v, useState as C, useCallback as n, useContext as S, createContext as m } from "react";
const M = m(void 0), $ = () => {
  const s = S(M);
  if (!s)
    throw new Error("useModalContext must be used within a ModalProvider");
  return s;
}, j = ({
  children: s
}) => {
  const d = v(/* @__PURE__ */ new Map()), [u, l] = C({
    growModals: [],
    slideModals: [],
    popModals: []
  }), p = n((e) => d.current.get(e), []), w = n((e) => k.getOrder(e), []), f = n((e, t, o) => {
    d.current.set(e, o), l((x) => {
      const r = {
        ...x
      }, a = (i, c) => {
        i.indexOf(c) >= 0 || i.push(c);
      };
      switch (t) {
        case "grow":
          a(r.growModals, e);
          break;
        case "slide":
          a(r.slideModals, e);
          break;
        case "pop":
          a(r.popModals, e);
          break;
        default:
          a(r.growModals, e);
      }
      return r;
    });
  }, []), g = n((e) => {
    d.current.delete(e), l((t) => ({
      growModals: t.growModals.filter((o) => o !== e),
      slideModals: t.slideModals.filter((o) => o !== e),
      popModals: t.popModals.filter((o) => o !== e)
    }));
  }, []), h = {
    getModal: p,
    modalRootState: u,
    addModal: f,
    removeModal: g,
    getModalOrder: w
  };
  return b(M.Provider, {
    value: h,
    children: s
  });
};
export {
  j as ModalProvider,
  $ as useModalContext
};
