import { o as Y } from "./rolldown-runtime.js";
import { i as $, t as z } from "./vendor/react.js";
import { s as _, t as T, u as J } from "./useRenderElement.js";
import { t as U } from "./useButton.js";
import { a as K, n as Q } from "./useTransitionStatus.js";
import { t as j } from "./useBaseUiId.js";
import { n as X, r as Z } from "./shadowDom.js";
import { A as ee, C as te, a as oe, b as ne, f as R, l as se, o as W, p as re, r as q, s as ae, t as N } from "./popupStateMapping.js";
import { i as ie, l as le, t as G } from "./createBaseUIEventDetails.js";
import { a as ue, i as ce, n as de, r as pe, t as ge } from "./useOpenInteractionType.js";
import { a as fe, i as me, n as Ce, o as Se, r as Re, t as he } from "./useSyncedFloatingRootContext.js";
import { t as ve } from "./useRole.js";
import { t as xe } from "./inertValue.js";
import { s as Oe } from "./composite2.js";
var r = /* @__PURE__ */ Y($()), L = /* @__PURE__ */ r.createContext(void 0);
function x(t) {
  const e = r.useContext(L);
  if (t === !1 && e === void 0) throw new Error(_(27));
  return e;
}
var Pe = {
  ...q,
  ...K
}, He = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { render: l, className: d, style: s, forceRender: u = !1, ...c } = e, { store: a } = x(), n = a.useState("open"), p = a.useState("nested"), m = a.useState("mounted");
  return T("div", e, {
    state: {
      open: n,
      transitionStatus: a.useState("transitionStatus")
    },
    ref: [a.context.backdropRef, o],
    stateAttributesMapping: Pe,
    props: [{
      role: "presentation",
      hidden: !m,
      style: {
        userSelect: "none",
        WebkitUserSelect: "none"
      }
    }, c],
    enabled: u || !p
  });
}), Ye = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { render: l, className: d, disabled: s = !1, nativeButton: u = !0, style: c, ...a } = e, { store: n } = x(), p = n.useState("open");
  function m(f) {
    p && n.setOpen(!1, G(ie, f.nativeEvent));
  }
  const { getButtonProps: C, buttonRef: g } = U({
    disabled: s,
    native: u
  });
  return T("button", e, {
    state: { disabled: s },
    ref: [o, g],
    props: [
      { onClick: m },
      a,
      C
    ]
  });
}), $e = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { render: l, className: d, style: s, id: u, ...c } = e, { store: a } = x(), n = j(u);
  return a.useSyncedValueWithCleanup("descriptionElementId", n), T("p", e, {
    ref: o,
    props: [{ id: n }, c]
  });
}), De = /* @__PURE__ */ (function(t) {
  return t.nestedDialogs = "--nested-dialogs", t;
})({}), Ie = (function(t) {
  return t[t.open = N.open] = "open", t[t.closed = N.closed] = "closed", t[t.startingStyle = N.startingStyle] = "startingStyle", t[t.endingStyle = N.endingStyle] = "endingStyle", t.nested = "data-nested", t.nestedDialogOpen = "data-nested-dialog-open", t;
})({}), A = /* @__PURE__ */ r.createContext(void 0);
function Ee() {
  const t = r.useContext(A);
  if (t === void 0) throw new Error(_(26));
  return t;
}
var b = z(), be = {
  ...q,
  ...K,
  nestedDialogOpen(t) {
    return t ? { [Ie.nestedDialogOpen]: "" } : null;
  }
}, ze = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { className: l, finalFocus: d, initialFocus: s, render: u, style: c, ...a } = e, { store: n } = x(), p = n.useState("descriptionElementId"), m = n.useState("disablePointerDismissal"), C = n.useState("floatingRootContext"), g = n.useState("popupProps"), f = n.useState("modal"), i = n.useState("mounted"), h = n.useState("nested"), S = n.useState("nestedOpenDialogCount"), v = n.useState("open"), y = n.useState("openMethod"), w = n.useState("titleElementId"), P = n.useState("transitionStatus"), D = n.useState("role");
  Ee(), Q({
    open: v,
    ref: n.context.popupRef,
    onComplete() {
      v && n.context.onOpenChangeComplete?.(!0);
    }
  });
  function I(E) {
    return E === "touch" ? n.context.popupRef.current : !0;
  }
  const k = s === void 0 ? I : s, B = T("div", e, {
    state: {
      open: v,
      nested: h,
      transitionStatus: P,
      nestedDialogOpen: S > 0
    },
    props: [
      g,
      {
        "aria-labelledby": w ?? void 0,
        "aria-describedby": p ?? void 0,
        role: D,
        tabIndex: -1,
        hidden: !i,
        onKeyDown(E) {
          Oe.has(E.key) && E.stopPropagation();
        },
        style: { [De.nestedDialogs]: S }
      },
      a
    ],
    ref: [
      o,
      n.context.popupRef,
      n.useStateSetter("popupElement")
    ],
    stateAttributesMapping: be
  });
  return /* @__PURE__ */ (0, b.jsx)(ue, {
    context: C,
    openInteractionType: y,
    disabled: !i,
    closeOnFocusOut: !m,
    initialFocus: k,
    returnFocus: d,
    modal: f !== !1,
    restoreFocus: "popup",
    children: B
  });
}), Je = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { keepMounted: l = !1, ...d } = e, { store: s } = x(), u = s.useState("mounted"), c = s.useState("modal"), a = s.useState("open");
  return u || l ? /* @__PURE__ */ (0, b.jsx)(A.Provider, {
    value: l,
    children: /* @__PURE__ */ (0, b.jsxs)(ne, {
      ref: o,
      ...d,
      children: [u && c === !0 && /* @__PURE__ */ (0, b.jsx)(pe, {
        ref: s.context.internalBackdropRef,
        inert: xe(!a)
      }), e.children]
    })
  }) : null;
});
function Te(t) {
  const { store: e, parentContext: o, actionsRef: l, isDrawer: d } = t, s = e.useState("open"), u = e.useState("disablePointerDismissal"), c = e.useState("modal"), a = e.useState("popupElement"), { openMethod: n, triggerProps: p } = ge(s);
  me(e);
  const { forceUnmount: m } = fe(s, e), C = r.useCallback(() => {
    e.setOpen(!1, G(le));
  }, [e]);
  r.useImperativeHandle(l, () => ({
    unmount: m,
    close: C
  }), [m, C]);
  const g = he({
    popupStore: e,
    onOpenChange: e.setOpen,
    treatPopupAsFloatingElement: !0
  }), [f, i] = r.useState(0), [h, S] = r.useState(0), v = f === 0, y = ve(g), w = re(g, {
    outsidePressEvent() {
      return e.context.internalBackdropRef.current || e.context.backdropRef.current ? "intentional" : {
        mouse: c === "trap-focus" ? "sloppy" : "intentional",
        touch: "sloppy"
      };
    },
    outsidePress(O) {
      if (!e.context.outsidePressEnabledRef.current || "button" in O && O.button !== 0 || "touches" in O && O.touches.length !== 1) return !1;
      const F = Z(O);
      if (v && !u) {
        const M = F;
        return c && (e.context.internalBackdropRef.current || e.context.backdropRef.current) ? e.context.internalBackdropRef.current === M || e.context.backdropRef.current === M || X(M, a) && !M?.hasAttribute("data-base-ui-portal") : !0;
      }
      return !1;
    },
    escapeKey: v
  });
  de(s && c === !0, a);
  const { getReferenceProps: P, getFloatingProps: D, getTriggerProps: I } = W([y, w]);
  e.useContextCallback("onNestedDialogOpen", (O, F) => {
    i(O), S(F);
  }), e.useContextCallback("onNestedDialogClose", () => {
    i(0), S(0);
  }), r.useEffect(() => (o?.onNestedDialogOpen && s && o.onNestedDialogOpen(f + 1, h + (d ? 1 : 0)), o?.onNestedDialogClose && !s && o.onNestedDialogClose(), () => {
    o?.onNestedDialogClose && s && o.onNestedDialogClose();
  }), [
    d,
    s,
    f,
    h,
    o
  ]);
  const k = r.useMemo(() => P(p), [P, p]), B = r.useMemo(() => I(p), [I, p]), E = r.useMemo(() => D(), [D]);
  e.useSyncedValues({
    openMethod: n,
    activeTriggerProps: k,
    inactiveTriggerProps: B,
    popupProps: E,
    floatingRootContext: g,
    nestedOpenDialogCount: f,
    nestedOpenDrawerCount: h
  });
}
var ye = {
  ...Re,
  modal: R((t) => t.modal),
  nested: R((t) => t.nested),
  nestedOpenDialogCount: R((t) => t.nestedOpenDialogCount),
  nestedOpenDrawerCount: R((t) => t.nestedOpenDrawerCount),
  disablePointerDismissal: R((t) => t.disablePointerDismissal),
  openMethod: R((t) => t.openMethod),
  descriptionElementId: R((t) => t.descriptionElementId),
  titleElementId: R((t) => t.titleElementId),
  viewportElement: R((t) => t.viewportElement),
  role: R((t) => t.role)
}, we = class H extends se {
  constructor(e) {
    super(ke(e), {
      popupRef: /* @__PURE__ */ r.createRef(),
      backdropRef: /* @__PURE__ */ r.createRef(),
      internalBackdropRef: /* @__PURE__ */ r.createRef(),
      outsidePressEnabledRef: { current: !0 },
      triggerElements: new ae(),
      onOpenChange: void 0,
      onOpenChangeComplete: void 0
    }, ye);
  }
  setOpen = (e, o) => {
    if (o.preventUnmountOnClose = () => {
      this.set("preventUnmountingOnClose", !0);
    }, !e && o.trigger == null && this.state.activeTriggerId != null && (o.trigger = this.state.activeTriggerElement ?? void 0), this.context.onOpenChange?.(e, o), o.isCanceled) return;
    this.state.floatingRootContext.dispatchOpenChange(e, o);
    const l = { open: e }, d = o.trigger?.id ?? null;
    (d || e) && (l.activeTriggerId = d, l.activeTriggerElement = o.trigger ?? null), this.update(l);
  };
  static useStore(e, o) {
    const l = J(() => new H(o)).current;
    return e ?? l;
  }
};
function ke(t = {}) {
  return {
    ...Ce(),
    modal: !0,
    disablePointerDismissal: !1,
    popupElement: null,
    viewportElement: null,
    descriptionElementId: void 0,
    titleElementId: void 0,
    openMethod: null,
    nested: !1,
    nestedOpenDialogCount: 0,
    nestedOpenDrawerCount: 0,
    role: "dialog",
    ...t
  };
}
var V = /* @__PURE__ */ r.createContext(!1);
function Qe(t) {
  const { children: e, open: o, defaultOpen: l = !1, onOpenChange: d, onOpenChangeComplete: s, disablePointerDismissal: u = !1, modal: c = !0, actionsRef: a, handle: n, triggerId: p, defaultTriggerId: m = null } = t, C = x(!0), g = r.useContext(V), f = !!C, i = we.useStore(n?.store, {
    open: l,
    openProp: o,
    activeTriggerId: m,
    triggerIdProp: p,
    modal: c,
    disablePointerDismissal: u,
    nested: f
  });
  ee(() => {
    o === void 0 && i.state.open === !1 && l === !0 && i.update({
      open: !0,
      activeTriggerId: m
    });
  }), i.useControlledProp("openProp", o), i.useControlledProp("triggerIdProp", p), i.useSyncedValues({
    disablePointerDismissal: u,
    nested: f,
    modal: c
  }), i.useContextCallback("onOpenChange", d), i.useContextCallback("onOpenChangeComplete", s);
  const h = i.useState("payload");
  Te({
    store: i,
    actionsRef: a,
    parentContext: C?.store.context,
    isDrawer: g,
    onOpenChange: d,
    triggerIdProp: p
  });
  const S = r.useMemo(() => ({ store: i }), [i]);
  return /* @__PURE__ */ (0, b.jsx)(V.Provider, {
    value: !1,
    children: /* @__PURE__ */ (0, b.jsx)(L.Provider, {
      value: S,
      children: typeof e == "function" ? e({ payload: h }) : e
    })
  });
}
var Xe = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { render: l, className: d, style: s, id: u, ...c } = e, { store: a } = x(), n = j(u);
  return a.useSyncedValueWithCleanup("titleElementId", n), T("h2", e, {
    ref: o,
    props: [{ id: n }, c]
  });
}), Ze = /* @__PURE__ */ r.forwardRef(function(e, o) {
  const { render: l, className: d, disabled: s = !1, nativeButton: u = !0, id: c, payload: a, handle: n, style: p, ...m } = e, C = x(!0), g = n?.store ?? C?.store;
  if (!g) throw new Error(_(79));
  const f = j(c), i = g.useState("floatingRootContext"), h = g.useState("isOpenedByTrigger", f), S = r.useRef(null), { registerTrigger: v, isMountedByThisTrigger: y } = Se(f, S, g, { payload: a }), { getButtonProps: w, buttonRef: P } = U({
    disabled: s,
    native: u
  }), D = W([ce(i, { enabled: i != null })]), I = {
    disabled: s,
    open: h
  }, k = g.useState("triggerProps", y);
  return T("button", e, {
    state: I,
    ref: [
      P,
      o,
      v,
      S
    ],
    props: [
      D.getReferenceProps(),
      k,
      {
        [te]: "",
        id: f
      },
      m,
      w
    ],
    stateAttributesMapping: oe
  });
});
export {
  we as a,
  ze as c,
  Ye as d,
  He as f,
  V as i,
  Ee as l,
  x as m,
  Xe as n,
  Te as o,
  L as p,
  Qe as r,
  Je as s,
  Ze as t,
  $e as u
};

//# sourceMappingURL=DialogTrigger.js.map