import * as u from "react";
import { jsx as _ } from "react/jsx-runtime";
function P(e, p = []) {
  let n = [];
  function m(s, t) {
    const o = u.createContext(t), c = n.length;
    n = [...n, t];
    const a = (i) => {
      const { scope: f, children: C, ...x } = i, d = f?.[e]?.[c] || o, v = u.useMemo(() => x, Object.values(x));
      return /* @__PURE__ */ _(d.Provider, { value: v, children: C });
    };
    a.displayName = s + "Provider";
    function S(i, f) {
      const C = f?.[e]?.[c] || o, x = u.useContext(C);
      if (x) return x;
      if (t !== void 0) return t;
      throw new Error(`\`${i}\` must be used within \`${s}\``);
    }
    return [a, S];
  }
  const r = () => {
    const s = n.map((t) => u.createContext(t));
    return function(o) {
      const c = o?.[e] || s;
      return u.useMemo(
        () => ({ [`__scope${e}`]: { ...o, [e]: c } }),
        [o, c]
      );
    };
  };
  return r.scopeName = e, [m, h(r, ...p)];
}
function h(...e) {
  const p = e[0];
  if (e.length === 1) return p;
  const n = () => {
    const m = e.map((r) => ({
      useScope: r(),
      scopeName: r.scopeName
    }));
    return function(s) {
      const t = m.reduce((o, { useScope: c, scopeName: a }) => {
        const i = c(s)[`__scope${a}`];
        return { ...o, ...i };
      }, {});
      return u.useMemo(() => ({ [`__scope${p.scopeName}`]: t }), [t]);
    };
  };
  return n.scopeName = p.scopeName, n;
}
export {
  P as createContextScope
};
//# sourceMappingURL=index.es.js.map
