import d from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as $, createElement as s, useRef as p, useEffect as M, Fragment as y, useCallback as F, Children as T } from "react";
import { composeEventHandlers as u } from "../../primitive/dist/index.js";
import { useComposedRefs as v } from "../../react-compose-refs/dist/index.js";
import { createContextScope as N } from "../../react-context/dist/index.js";
import { useId as _ } from "../../react-id/dist/index.js";
import { useControllableState as S } from "../../react-use-controllable-state/dist/index.js";
import { DismissableLayer as k } from "../../react-dismissable-layer/dist/index.js";
import { FocusScope as L } from "../../react-focus-scope/dist/index.js";
import { Portal as w } from "../../react-portal/dist/index.js";
import { Presence as C } from "../../react-presence/dist/index.js";
import { Primitive as b } from "../../react-primitive/dist/index.js";
import { useFocusGuards as j } from "../../react-focus-guards/dist/index.js";
import G from "../../../react-remove-scroll/dist/es2015/Combination.js";
import { hideOthers as H } from "../../../aria-hidden/dist/es2015/index.js";
import { Slot as K } from "../../react-slot/dist/index.js";
const P = "Dialog", [R, Ce] = N(P), [U, l] = R(P), V = (e) => {
  const { __scopeDialog: r, children: c, open: n, defaultOpen: t, onOpenChange: o, modal: a = !0 } = e, i = p(null), f = p(null), [D = !1, m] = S({
    prop: n,
    defaultProp: t,
    onChange: o
  });
  return /* @__PURE__ */ s(U, {
    scope: r,
    triggerRef: i,
    contentRef: f,
    contentId: _(),
    titleId: _(),
    descriptionId: _(),
    open: D,
    onOpenChange: m,
    onOpenToggle: F(
      () => m(
        (A) => !A
      ),
      [
        m
      ]
    ),
    modal: a
  }, c);
}, Y = "DialogTrigger", Z = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, ...n } = e, t = l(Y, c), o = v(r, t.triggerRef);
  return /* @__PURE__ */ s(b.button, d({
    type: "button",
    "aria-haspopup": "dialog",
    "aria-expanded": t.open,
    "aria-controls": t.contentId,
    "data-state": O(t.open)
  }, n, {
    ref: o,
    onClick: u(e.onClick, t.onOpenToggle)
  }));
}), E = "DialogPortal", [q, h] = R(E, {
  forceMount: void 0
}), z = (e) => {
  const { __scopeDialog: r, forceMount: c, children: n, container: t } = e, o = l(E, r);
  return /* @__PURE__ */ s(q, {
    scope: r,
    forceMount: c
  }, T.map(
    n,
    (a) => /* @__PURE__ */ s(C, {
      present: c || o.open
    }, /* @__PURE__ */ s(w, {
      asChild: !0,
      container: t
    }, a))
  ));
}, x = "DialogOverlay", B = /* @__PURE__ */ $((e, r) => {
  const c = h(x, e.__scopeDialog), { forceMount: n = c.forceMount, ...t } = e, o = l(x, e.__scopeDialog);
  return o.modal ? /* @__PURE__ */ s(C, {
    present: n || o.open
  }, /* @__PURE__ */ s(J, d({}, t, {
    ref: r
  }))) : null;
}), J = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, ...n } = e, t = l(x, c);
  return (
    // Make sure `Content` is scrollable even when it doesn't live inside `RemoveScroll`
    // ie. when `Overlay` and `Content` are siblings
    /* @__PURE__ */ s(G, {
      as: K,
      allowPinchZoom: !0,
      shards: [
        t.contentRef
      ]
    }, /* @__PURE__ */ s(b.div, d({
      "data-state": O(t.open)
    }, n, {
      ref: r,
      style: {
        pointerEvents: "auto",
        ...n.style
      }
    })))
  );
}), g = "DialogContent", Q = /* @__PURE__ */ $((e, r) => {
  const c = h(g, e.__scopeDialog), { forceMount: n = c.forceMount, ...t } = e, o = l(g, e.__scopeDialog);
  return /* @__PURE__ */ s(C, {
    present: n || o.open
  }, o.modal ? /* @__PURE__ */ s(W, d({}, t, {
    ref: r
  })) : /* @__PURE__ */ s(X, d({}, t, {
    ref: r
  })));
}), W = /* @__PURE__ */ $((e, r) => {
  const c = l(g, e.__scopeDialog), n = p(null), t = v(r, c.contentRef, n);
  return M(() => {
    const o = n.current;
    if (o)
      return H(o);
  }, []), /* @__PURE__ */ s(I, d({}, e, {
    ref: t,
    trapFocus: c.open,
    disableOutsidePointerEvents: !0,
    onCloseAutoFocus: u(e.onCloseAutoFocus, (o) => {
      var a;
      o.preventDefault(), (a = c.triggerRef.current) === null || a === void 0 || a.focus();
    }),
    onPointerDownOutside: u(e.onPointerDownOutside, (o) => {
      const a = o.detail.originalEvent, i = a.button === 0 && a.ctrlKey === !0;
      (a.button === 2 || i) && o.preventDefault();
    }),
    onFocusOutside: u(
      e.onFocusOutside,
      (o) => o.preventDefault()
    )
  }));
}), X = /* @__PURE__ */ $((e, r) => {
  const c = l(g, e.__scopeDialog), n = p(!1), t = p(!1);
  return /* @__PURE__ */ s(I, d({}, e, {
    ref: r,
    trapFocus: !1,
    disableOutsidePointerEvents: !1,
    onCloseAutoFocus: (o) => {
      var a;
      if ((a = e.onCloseAutoFocus) === null || a === void 0 || a.call(e, o), !o.defaultPrevented) {
        var i;
        n.current || (i = c.triggerRef.current) === null || i === void 0 || i.focus(), o.preventDefault();
      }
      n.current = !1, t.current = !1;
    },
    onInteractOutside: (o) => {
      var a, i;
      (a = e.onInteractOutside) === null || a === void 0 || a.call(e, o), o.defaultPrevented || (n.current = !0, o.detail.originalEvent.type === "pointerdown" && (t.current = !0));
      const f = o.target;
      ((i = c.triggerRef.current) === null || i === void 0 ? void 0 : i.contains(f)) && o.preventDefault(), o.detail.originalEvent.type === "focusin" && t.current && o.preventDefault();
    }
  }));
}), I = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, trapFocus: n, onOpenAutoFocus: t, onCloseAutoFocus: o, ...a } = e, i = l(g, c), f = p(null), D = v(r, f);
  return j(), /* @__PURE__ */ s(y, null, /* @__PURE__ */ s(L, {
    asChild: !0,
    loop: !0,
    trapped: n,
    onMountAutoFocus: t,
    onUnmountAutoFocus: o
  }, /* @__PURE__ */ s(k, d({
    role: "dialog",
    id: i.contentId,
    "aria-describedby": i.descriptionId,
    "aria-labelledby": i.titleId,
    "data-state": O(i.open)
  }, a, {
    ref: D,
    onDismiss: () => i.onOpenChange(!1)
  }))), !1);
}), ee = "DialogTitle", oe = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, ...n } = e, t = l(ee, c);
  return /* @__PURE__ */ s(b.h2, d({
    id: t.titleId
  }, n, {
    ref: r
  }));
}), te = "DialogDescription", ce = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, ...n } = e, t = l(te, c);
  return /* @__PURE__ */ s(b.p, d({
    id: t.descriptionId
  }, n, {
    ref: r
  }));
}), ne = "DialogClose", re = /* @__PURE__ */ $((e, r) => {
  const { __scopeDialog: c, ...n } = e, t = l(ne, c);
  return /* @__PURE__ */ s(b.button, d({
    type: "button"
  }, n, {
    ref: r,
    onClick: u(
      e.onClick,
      () => t.onOpenChange(!1)
    )
  }));
});
function O(e) {
  return e ? "open" : "closed";
}
const Oe = V, Pe = Z, Re = z, Ee = B, he = Q, Ie = oe, Ae = ce, Me = re;
export {
  Me as Close,
  he as Content,
  Ae as Description,
  V as Dialog,
  re as DialogClose,
  Q as DialogContent,
  ce as DialogDescription,
  B as DialogOverlay,
  z as DialogPortal,
  oe as DialogTitle,
  Z as DialogTrigger,
  Ee as Overlay,
  Re as Portal,
  Oe as Root,
  Ie as Title,
  Pe as Trigger,
  Ce as createDialogScope
};
