import { jsx as c, Fragment as j } from "react/jsx-runtime";
import * as a from "react";
import { useContext as d, createContext as f, useMemo as x, forwardRef as A, useRef as h, useCallback as g, useState as N, useEffect as C, Children as W } from "react";
const D = f({}), X = ({
  children: e,
  portalContainer: t
}) => /* @__PURE__ */ c(D.Provider, { value: { portalContainer: t }, children: e }), Y = () => d(D);
var E = /* @__PURE__ */ ((e) => (e.Dark = "dark", e.Light = "light", e.System = "system", e))(E || {}), $ = /* @__PURE__ */ ((e) => (e.Standard = "std", e.Tritanopia = "tri", e.ProtanopiaAndDeuteranopia = "pnd", e))($ || {}), k = /* @__PURE__ */ ((e) => (e.Standard = "std", e.Low = "low", e.High = "high", e))(k || {});
const w = "dark-std-std", y = f({
  theme: w,
  setTheme: () => {
  },
  isLightTheme: !1
}), ee = ({ children: e, theme: t, ...r }) => {
  const n = t ?? w, o = x(() => n.includes(E.Light), [n]);
  return /* @__PURE__ */ c(y.Provider, { value: { ...r, theme: n, isLightTheme: o }, children: e });
}, te = () => {
  const e = d(y);
  if (!e)
    throw new Error("useTheme must be used within ThemeProvider");
  return e;
};
function I(e, t) {
  return (t == null ? void 0 : t.defaultValue) ?? "";
}
const R = f({
  t: (e, t = "", r) => I(e, { ...r, defaultValue: t })
}), M = (e) => ({
  t: (t, r = "", n) => e(t, { ...n, defaultValue: r }) || r
});
function re({ t: e = I, children: t }) {
  const r = x(() => M(e), [e]);
  return /* @__PURE__ */ c(R.Provider, { value: r, children: t });
}
function ne() {
  return d(R);
}
function P(e, t) {
  if (typeof e == "function")
    return e(t);
  e != null && (e.current = t);
}
function S(...e) {
  return (t) => {
    let r = !1;
    const n = e.map((o) => {
      const s = P(o, t);
      return !r && typeof s == "function" && (r = !0), s;
    });
    if (r)
      return () => {
        for (let o = 0; o < n.length; o++) {
          const s = n[o];
          typeof s == "function" ? s() : P(e[o], null);
        }
      };
  };
}
function oe(...e) {
  return a.useCallback(S(...e), e);
}
var b = a.forwardRef((e, t) => {
  const { children: r, ...n } = e, o = a.Children.toArray(r), s = o.find(H);
  if (s) {
    const i = s.props.children, l = o.map((u) => u === s ? a.Children.count(i) > 1 ? a.Children.only(null) : a.isValidElement(i) ? i.props.children : null : u);
    return /* @__PURE__ */ c(m, { ...n, ref: t, children: a.isValidElement(i) ? a.cloneElement(i, void 0, l) : null });
  }
  return /* @__PURE__ */ c(m, { ...n, ref: t, children: r });
});
b.displayName = "Slot";
var m = a.forwardRef((e, t) => {
  const { children: r, ...n } = e;
  if (a.isValidElement(r)) {
    const o = _(r), s = Z(n, r.props);
    return r.type !== a.Fragment && (s.ref = t ? S(t, o) : o), a.cloneElement(r, s);
  }
  return a.Children.count(r) > 1 ? a.Children.only(null) : null;
});
m.displayName = "SlotClone";
var B = ({ children: e }) => /* @__PURE__ */ c(j, { children: e });
function H(e) {
  return a.isValidElement(e) && e.type === B;
}
function Z(e, t) {
  const r = { ...t };
  for (const n in t) {
    const o = e[n], s = t[n];
    /^on[A-Z]/.test(n) ? o && s ? r[n] = (...l) => {
      s(...l), o(...l);
    } : o && (r[n] = o) : n === "style" ? r[n] = { ...o, ...s } : n === "className" && (r[n] = [o, s].filter(Boolean).join(" "));
  }
  return { ...e, ...r };
}
function _(e) {
  var n, o;
  let t = (n = Object.getOwnPropertyDescriptor(e.props, "ref")) == null ? void 0 : n.get, r = t && "isReactWarning" in t && t.isReactWarning;
  return r ? e.ref : (t = (o = Object.getOwnPropertyDescriptor(e, "ref")) == null ? void 0 : o.get, r = t && "isReactWarning" in t && t.isReactWarning, r ? e.props.ref : e.props.ref || e.ref);
}
const V = f(void 0), p = () => d(V), se = ({ children: e }) => {
  const t = h([]), r = g((i) => {
    t.current = t.current.filter((l) => l.triggerId !== i);
  }, []), n = g((i) => {
    r(i.triggerId), t.current.push(i);
  }, []), o = g((i) => {
    const l = t.current.findIndex((u) => u.triggerId === i);
    if (l !== -1) {
      const u = t.current[l];
      u.triggerElement && setTimeout(() => u.triggerElement.focus(), 0), r(u.triggerId);
    } else {
      const u = t.current.at(-1);
      u != null && u.triggerElement && setTimeout(() => u.triggerElement.focus(), 0);
    }
  }, []), s = g(() => t.current.at(-1), []);
  return /* @__PURE__ */ c(V.Provider, { value: { registerTrigger: n, unregisterTrigger: r, restoreFocus: o, getLastTrigger: s }, children: e });
};
let q = 0;
const O = (e) => h(`${e || "dialog-trigger"}-${q++}`).current, ie = () => {
  const e = p(), t = O(), r = h(null), n = g(() => {
    e && r.current && e.registerTrigger({ triggerId: t, triggerElement: r.current });
  }, [e, t]);
  return { triggerRef: r, registerTrigger: n };
}, L = f(void 0), z = () => {
  const e = d(L);
  if (!e)
    throw new Error("useDialogOpenContext must be used within a DialogProvider");
  return e;
}, ae = () => {
  const e = p(), [t, r] = N(""), { open: n } = z(), o = g(() => {
    e && e.restoreFocus(t);
  }, [e, t]);
  return C(() => {
    var s;
    if (e && n) {
      const i = (s = e.getLastTrigger()) == null ? void 0 : s.triggerId;
      if (!i) return;
      r(i);
    }
  }, [n]), C(() => () => {
    e && e.unregisterTrigger(t);
  }, []), { handleCloseAutoFocus: o };
}, G = A(({ onClick: e, id: t, children: r, TriggerComponent: n, ...o }, s) => {
  const i = O(t), l = p(), F = d(L) !== void 0;
  W.count(r) > 1 && (console.warn("Dialog.Trigger: Only one child is allowed"), r = /* @__PURE__ */ c("span", { children: r }));
  const T = /* @__PURE__ */ c(b, { ref: s, onClick: (v) => {
    l && l.registerTrigger({ triggerId: i, triggerElement: v.currentTarget }), e == null || e(v);
  }, ...o, id: i, children: r });
  return F && !e ? /* @__PURE__ */ c(n, { asChild: !0, children: T }) : T;
});
G.displayName = "DialogTriggerBase";
export {
  k as C,
  L as D,
  E as M,
  X as P,
  b as S,
  G as T,
  ie as a,
  ne as b,
  oe as c,
  B as d,
  Y as e,
  ae as f,
  S as g,
  $ as h,
  se as i,
  ee as j,
  re as k,
  w as l,
  M as m,
  te as u
};
//# sourceMappingURL=dialog-context-YQHn5wn_.js.map
