"use client";
import * as r from "react";
import { u as L } from "./useStableCallback-CudqoENs.js";
import { u as q, a as z, F as H, I as Y, b as $ } from "./InternalBackdrop-BVEfe6O1.js";
import { c as _, i as A, d as J } from "./createBaseUIEventDetails-CqidROtw.js";
import { a as Q, b as X, u as Z, p as ee, c as te, d as oe } from "./useSyncedFloatingRootContext-Bc90zSTy.js";
import { u as ne } from "./useRole-BQ8R4yU3.js";
import { p as se, q as U, R as re, P as ae, d as S, x as K, y as w, z as ie, C as le, r as ce } from "./popupStateMapping-BT9Y_jIq.js";
import { b as ue, c as de } from "./element-DSo91iOi.js";
import { f as B, u as I } from "./useRenderElement-DNB2vtF1.js";
import { t as W, a as pe } from "./useOpenChangeComplete-DkeH9FGJ.js";
import { a as j } from "./useButton-JYPKQKj4.js";
import { u as F } from "./useBaseUiId-C8Dq0BQq.js";
import { C as ge } from "./composite-BSpUD7nI.js";
import { jsx as M, jsxs as fe } from "react/jsx-runtime";
import { i as me } from "./inertValue-kpZZDBEy.js";
function qe(t) {
  const {
    store: e,
    parentContext: n,
    actionsRef: d
  } = t, a = e.useState("open"), s = e.useState("disablePointerDismissal"), l = e.useState("modal"), i = e.useState("popupElement"), {
    openMethod: o,
    triggerProps: c
  } = q(a);
  Q(e);
  const {
    forceUnmount: g
  } = X(a, e), f = L((C) => {
    const b = _(C);
    return b.preventUnmountOnClose = () => {
      e.set("preventUnmountingOnClose", !0);
    }, b;
  }), u = r.useCallback(() => {
    e.setOpen(!1, f(A));
  }, [e, f]);
  r.useImperativeHandle(d, () => ({
    unmount: g,
    close: u
  }), [g, u]);
  const m = Z({
    popupStore: e,
    onOpenChange: e.setOpen,
    treatPopupAsFloatingElement: !0,
    noEmit: !0
  }), [p, N] = r.useState(0), D = p === 0, R = ne(m), v = se(m, {
    outsidePressEvent() {
      return e.context.internalBackdropRef.current || e.context.backdropRef.current ? "intentional" : {
        mouse: l === "trap-focus" ? "sloppy" : "intentional",
        touch: "sloppy"
      };
    },
    outsidePress(C) {
      if (!e.context.outsidePressEnabledRef.current || "button" in C && C.button !== 0 || "touches" in C && C.touches.length !== 1)
        return !1;
      const b = ue(C);
      if (D && !s) {
        const E = b;
        return l && (e.context.internalBackdropRef.current || e.context.backdropRef.current) ? e.context.internalBackdropRef.current === E || e.context.backdropRef.current === E || de(E, i) && !E?.hasAttribute("data-base-ui-portal") : !0;
      }
      return !1;
    },
    escapeKey: D
  });
  z(a && l === !0, i);
  const {
    getReferenceProps: O,
    getFloatingProps: P,
    getTriggerProps: x
  } = U([R, v]);
  e.useContextCallback("onNestedDialogOpen", (C) => {
    N(C + 1);
  }), e.useContextCallback("onNestedDialogClose", () => {
    N(0);
  }), r.useEffect(() => (n?.onNestedDialogOpen && a && n.onNestedDialogOpen(p), n?.onNestedDialogClose && !a && n.onNestedDialogClose(), () => {
    n?.onNestedDialogClose && a && n.onNestedDialogClose();
  }), [a, n, p]);
  const y = r.useMemo(() => O(c), [O, c]), T = r.useMemo(() => x(c), [x, c]), k = r.useMemo(() => P(), [P]);
  e.useSyncedValues({
    openMethod: o,
    activeTriggerProps: y,
    inactiveTriggerProps: T,
    popupProps: k,
    floatingRootContext: m,
    nestedOpenDialogCount: p
  });
}
const G = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (G.displayName = "DialogRootContext");
function h(t) {
  const e = r.useContext(G);
  if (t === !1 && e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: DialogRootContext is missing. Dialog parts must be placed within <Dialog.Root>." : B(27));
  return e;
}
const Ce = {
  ...ee,
  modal: S((t) => t.modal),
  nested: S((t) => t.nested),
  nestedOpenDialogCount: S((t) => t.nestedOpenDialogCount),
  disablePointerDismissal: S((t) => t.disablePointerDismissal),
  openMethod: S((t) => t.openMethod),
  descriptionElementId: S((t) => t.descriptionElementId),
  titleElementId: S((t) => t.titleElementId),
  viewportElement: S((t) => t.viewportElement),
  role: S((t) => t.role)
};
class ze extends re {
  constructor(e) {
    super(Ee(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
    }, Ce);
  }
  setOpen = (e, n) => {
    if (n.preventUnmountOnClose = () => {
      this.set("preventUnmountingOnClose", !0);
    }, !e && n.trigger == null && this.state.activeTriggerId != null && (n.trigger = this.state.activeTriggerElement ?? void 0), this.context.onOpenChange?.(e, n), n.isCanceled)
      return;
    const d = {
      open: e,
      nativeEvent: n.event,
      reason: n.reason,
      nested: this.state.nested
    };
    this.state.floatingRootContext.context.events?.emit("openchange", d);
    const a = {
      open: e
    }, s = n.trigger?.id ?? null;
    (s || e) && (a.activeTriggerId = s, a.activeTriggerElement = n.trigger ?? null), this.update(a);
  };
}
function Ee(t = {}) {
  return {
    ...te(),
    modal: !0,
    disablePointerDismissal: !1,
    popupElement: null,
    viewportElement: null,
    descriptionElementId: void 0,
    titleElementId: void 0,
    openMethod: null,
    nested: !1,
    nestedOpenDialogCount: 0,
    role: "dialog",
    ...t
  };
}
const Se = {
  ...K,
  ...W
}, De = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    render: d,
    className: a,
    forceRender: s = !1,
    ...l
  } = e, {
    store: i
  } = h(), o = i.useState("open"), c = i.useState("nested"), g = i.useState("mounted"), f = i.useState("transitionStatus");
  return I("div", e, {
    state: {
      open: o,
      transitionStatus: f
    },
    ref: [i.context.backdropRef, n],
    stateAttributesMapping: Se,
    props: [{
      role: "presentation",
      hidden: !g,
      style: {
        userSelect: "none",
        WebkitUserSelect: "none"
      }
    }, l],
    enabled: s || !c
  });
});
process.env.NODE_ENV !== "production" && (De.displayName = "DialogBackdrop");
const Re = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    render: d,
    className: a,
    disabled: s = !1,
    nativeButton: l = !0,
    ...i
  } = e, {
    store: o
  } = h(), c = o.useState("open");
  function g(p) {
    c && o.setOpen(!1, _(J, p.nativeEvent));
  }
  const {
    getButtonProps: f,
    buttonRef: u
  } = j({
    disabled: s,
    native: l
  });
  return I("button", e, {
    state: {
      disabled: s
    },
    ref: [n, u],
    props: [{
      onClick: g
    }, i, f]
  });
});
process.env.NODE_ENV !== "production" && (Re.displayName = "DialogClose");
const he = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    render: d,
    className: a,
    id: s,
    ...l
  } = e, {
    store: i
  } = h(), o = F(s);
  return i.useSyncedValueWithCleanup("descriptionElementId", o), I("p", e, {
    ref: n,
    props: [{
      id: o
    }, l]
  });
});
process.env.NODE_ENV !== "production" && (he.displayName = "DialogDescription");
let Ne = /* @__PURE__ */ (function(t) {
  return t.nestedDialogs = "--nested-dialogs", t;
})({}), Oe = (function(t) {
  return t[t.open = w.open] = "open", t[t.closed = w.closed] = "closed", t[t.startingStyle = w.startingStyle] = "startingStyle", t[t.endingStyle = w.endingStyle] = "endingStyle", t.nested = "data-nested", t.nestedDialogOpen = "data-nested-dialog-open", t;
})({});
const V = /* @__PURE__ */ r.createContext(void 0);
process.env.NODE_ENV !== "production" && (V.displayName = "DialogPortalContext");
function Pe() {
  const t = r.useContext(V);
  if (t === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <Dialog.Portal> is missing." : B(26));
  return t;
}
const xe = {
  ...K,
  ...W,
  nestedDialogOpen(t) {
    return t ? {
      [Oe.nestedDialogOpen]: ""
    } : null;
  }
}, be = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    className: d,
    finalFocus: a,
    initialFocus: s,
    render: l,
    ...i
  } = e, {
    store: o
  } = h(), c = o.useState("descriptionElementId"), g = o.useState("disablePointerDismissal"), f = o.useState("floatingRootContext"), u = o.useState("popupProps"), m = o.useState("modal"), p = o.useState("mounted"), N = o.useState("nested"), D = o.useState("nestedOpenDialogCount"), R = o.useState("open"), v = o.useState("openMethod"), O = o.useState("titleElementId"), P = o.useState("transitionStatus"), x = o.useState("role");
  Pe(), pe({
    open: R,
    ref: o.context.popupRef,
    onComplete() {
      R && o.context.onOpenChangeComplete?.(!0);
    }
  });
  function y(E) {
    return E === "touch" ? o.context.popupRef.current : !0;
  }
  const T = s === void 0 ? y : s, k = D > 0, b = I("div", e, {
    state: {
      open: R,
      nested: N,
      transitionStatus: P,
      nestedDialogOpen: k
    },
    props: [u, {
      "aria-labelledby": O ?? void 0,
      "aria-describedby": c ?? void 0,
      role: x,
      tabIndex: -1,
      hidden: !p,
      onKeyDown(E) {
        ge.has(E.key) && E.stopPropagation();
      },
      style: {
        [Ne.nestedDialogs]: D
      }
    }, i],
    ref: [n, o.context.popupRef, o.useStateSetter("popupElement")],
    stateAttributesMapping: xe
  });
  return /* @__PURE__ */ M(H, {
    context: f,
    openInteractionType: v,
    disabled: !p,
    closeOnFocusOut: !g,
    initialFocus: T,
    returnFocus: a,
    modal: m !== !1,
    restoreFocus: "popup",
    children: b
  });
});
process.env.NODE_ENV !== "production" && (be.displayName = "DialogPopup");
const Ie = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    keepMounted: d = !1,
    ...a
  } = e, {
    store: s
  } = h(), l = s.useState("mounted"), i = s.useState("modal"), o = s.useState("open");
  return l || d ? /* @__PURE__ */ M(V.Provider, {
    value: d,
    children: /* @__PURE__ */ fe(ie, {
      ref: n,
      ...a,
      children: [l && i === !0 && /* @__PURE__ */ M(Y, {
        ref: s.context.internalBackdropRef,
        inert: me(!o)
      }), e.children]
    })
  }) : null;
});
process.env.NODE_ENV !== "production" && (Ie.displayName = "DialogPortal");
const ve = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    render: d,
    className: a,
    id: s,
    ...l
  } = e, {
    store: i
  } = h(), o = F(s);
  return i.useSyncedValueWithCleanup("titleElementId", o), I("h2", e, {
    ref: n,
    props: [{
      id: o
    }, l]
  });
});
process.env.NODE_ENV !== "production" && (ve.displayName = "DialogTitle");
const ye = /* @__PURE__ */ r.forwardRef(function(e, n) {
  const {
    render: d,
    className: a,
    disabled: s = !1,
    nativeButton: l = !0,
    id: i,
    payload: o,
    handle: c,
    ...g
  } = e, f = h(!0), u = c?.store ?? f?.store;
  if (!u)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <Dialog.Trigger> must be used within <Dialog.Root> or provided with a handle." : B(79));
  const m = F(i), p = u.useState("floatingRootContext"), N = u.useState("isOpenedByTrigger", m), D = r.useRef(null), {
    registerTrigger: R,
    isMountedByThisTrigger: v
  } = oe(m, D, u, {
    payload: o
  }), {
    getButtonProps: O,
    buttonRef: P
  } = j({
    disabled: s,
    native: l
  }), x = $(p, {
    enabled: p != null
  }), y = U([x]), T = {
    disabled: s,
    open: N
  }, k = u.useState("triggerProps", v);
  return I("button", e, {
    state: T,
    ref: [P, n, R, D],
    props: [y.getReferenceProps(), k, {
      [le]: "",
      id: m
    }, g, O],
    stateAttributesMapping: ce
  });
});
process.env.NODE_ENV !== "production" && (ye.displayName = "DialogTrigger");
export {
  Re as D,
  be as a,
  he as b,
  De as c,
  Ie as d,
  ve as e,
  ye as f,
  ze as g,
  qe as h,
  G as i,
  h as u
};
