"use client";
import * as c from "react";
import { composeEventHandlers as P } from "../../primitive/dist/index.js";
import { useComposedRefs as O } from "../../react-compose-refs/dist/index.js";
import { createContextScope as L } from "../../react-context/dist/index.js";
import { DismissableLayer as G } from "../../react-dismissable-layer/dist/index.js";
import { useFocusGuards as H } from "../../react-focus-guards/dist/index.js";
import { FocusScope as K } from "../../react-focus-scope/dist/index.js";
import { useId as $ } from "../../react-id/dist/index.js";
import { createPopperScope as _, Root as j, Anchor as x, Content as U, Arrow as V } from "../../react-popper/dist/index.js";
import { Portal as W } from "../../react-portal/dist/index.js";
import { Presence as w } from "../../react-presence/dist/index.js";
import { Primitive as E } from "../../react-primitive/dist/index.js";
import { createSlot as Z } from "../node_modules/@radix-ui/react-slot/dist/index.js";
import { useControllableState as q } from "../../react-use-controllable-state/dist/index.js";
import { hideOthers as z } from "../../../aria-hidden/dist/es2015/index.js";
import B from "../../../react-remove-scroll/dist/es2015/Combination.js";
import { jsx as p } from "react/jsx-runtime";
var C = "Popover", [b] = L(C, [
  _
]), m = _(), [J, u] = b(C), F = (o) => {
  const {
    __scopePopover: n,
    children: t,
    open: a,
    defaultOpen: e,
    onOpenChange: r,
    modal: s = !1
  } = o, i = m(n), d = c.useRef(null), [v, g] = c.useState(!1), [h, l] = q({
    prop: a,
    defaultProp: e ?? !1,
    onChange: r,
    caller: C
  });
  return /* @__PURE__ */ p(j, { ...i, children: /* @__PURE__ */ p(
    J,
    {
      scope: n,
      contentId: $(),
      triggerRef: d,
      open: h,
      onOpenChange: l,
      onOpenToggle: c.useCallback(() => l((R) => !R), [l]),
      hasCustomAnchor: v,
      onCustomAnchorAdd: c.useCallback(() => g(!0), []),
      onCustomAnchorRemove: c.useCallback(() => g(!1), []),
      modal: s,
      children: t
    }
  ) });
};
F.displayName = C;
var N = "PopoverAnchor", Q = c.forwardRef(
  (o, n) => {
    const { __scopePopover: t, ...a } = o, e = u(N, t), r = m(t), { onCustomAnchorAdd: s, onCustomAnchorRemove: i } = e;
    return c.useEffect(() => (s(), () => i()), [s, i]), /* @__PURE__ */ p(x, { ...r, ...a, ref: n });
  }
);
Q.displayName = N;
var S = "PopoverTrigger", y = c.forwardRef(
  (o, n) => {
    const { __scopePopover: t, ...a } = o, e = u(S, t), r = m(t), s = O(n, e.triggerRef), i = /* @__PURE__ */ p(
      E.button,
      {
        type: "button",
        "aria-haspopup": "dialog",
        "aria-expanded": e.open,
        "aria-controls": e.contentId,
        "data-state": T(e.open),
        ...a,
        ref: s,
        onClick: P(o.onClick, e.onOpenToggle)
      }
    );
    return e.hasCustomAnchor ? i : /* @__PURE__ */ p(x, { asChild: !0, ...r, children: i });
  }
);
y.displayName = S;
var A = "PopoverPortal", [X, Y] = b(A, {
  forceMount: void 0
}), D = (o) => {
  const { __scopePopover: n, forceMount: t, children: a, container: e } = o, r = u(A, n);
  return /* @__PURE__ */ p(X, { scope: n, forceMount: t, children: /* @__PURE__ */ p(w, { present: t || r.open, children: /* @__PURE__ */ p(W, { asChild: !0, container: e, children: a }) }) });
};
D.displayName = A;
var f = "PopoverContent", M = c.forwardRef(
  (o, n) => {
    const t = Y(f, o.__scopePopover), { forceMount: a = t.forceMount, ...e } = o, r = u(f, o.__scopePopover);
    return /* @__PURE__ */ p(w, { present: a || r.open, children: r.modal ? /* @__PURE__ */ p(eo, { ...e, ref: n }) : /* @__PURE__ */ p(ro, { ...e, ref: n }) });
  }
);
M.displayName = f;
var oo = Z("PopoverContent.RemoveScroll"), eo = c.forwardRef(
  (o, n) => {
    const t = u(f, o.__scopePopover), a = c.useRef(null), e = O(n, a), r = c.useRef(!1);
    return c.useEffect(() => {
      const s = a.current;
      if (s) return z(s);
    }, []), /* @__PURE__ */ p(B, { as: oo, allowPinchZoom: !0, children: /* @__PURE__ */ p(
      k,
      {
        ...o,
        ref: e,
        trapFocus: t.open,
        disableOutsidePointerEvents: !0,
        onCloseAutoFocus: P(o.onCloseAutoFocus, (s) => {
          s.preventDefault(), r.current || t.triggerRef.current?.focus();
        }),
        onPointerDownOutside: P(
          o.onPointerDownOutside,
          (s) => {
            const i = s.detail.originalEvent, d = i.button === 0 && i.ctrlKey === !0, v = i.button === 2 || d;
            r.current = v;
          },
          { checkForDefaultPrevented: !1 }
        ),
        onFocusOutside: P(
          o.onFocusOutside,
          (s) => s.preventDefault(),
          { checkForDefaultPrevented: !1 }
        )
      }
    ) });
  }
), ro = c.forwardRef(
  (o, n) => {
    const t = u(f, o.__scopePopover), a = c.useRef(!1), e = c.useRef(!1);
    return /* @__PURE__ */ p(
      k,
      {
        ...o,
        ref: n,
        trapFocus: !1,
        disableOutsidePointerEvents: !1,
        onCloseAutoFocus: (r) => {
          o.onCloseAutoFocus?.(r), r.defaultPrevented || (a.current || t.triggerRef.current?.focus(), r.preventDefault()), a.current = !1, e.current = !1;
        },
        onInteractOutside: (r) => {
          o.onInteractOutside?.(r), r.defaultPrevented || (a.current = !0, r.detail.originalEvent.type === "pointerdown" && (e.current = !0));
          const s = r.target;
          t.triggerRef.current?.contains(s) && r.preventDefault(), r.detail.originalEvent.type === "focusin" && e.current && r.preventDefault();
        }
      }
    );
  }
), k = c.forwardRef(
  (o, n) => {
    const {
      __scopePopover: t,
      trapFocus: a,
      onOpenAutoFocus: e,
      onCloseAutoFocus: r,
      disableOutsidePointerEvents: s,
      onEscapeKeyDown: i,
      onPointerDownOutside: d,
      onFocusOutside: v,
      onInteractOutside: g,
      ...h
    } = o, l = u(f, t), R = m(t);
    return H(), /* @__PURE__ */ p(
      K,
      {
        asChild: !0,
        loop: !0,
        trapped: a,
        onMountAutoFocus: e,
        onUnmountAutoFocus: r,
        children: /* @__PURE__ */ p(
          G,
          {
            asChild: !0,
            disableOutsidePointerEvents: s,
            onInteractOutside: g,
            onEscapeKeyDown: i,
            onPointerDownOutside: d,
            onFocusOutside: v,
            onDismiss: () => l.onOpenChange(!1),
            children: /* @__PURE__ */ p(
              U,
              {
                "data-state": T(l.open),
                role: "dialog",
                id: l.contentId,
                ...R,
                ...h,
                ref: n,
                style: {
                  ...h.style,
                  "--radix-popover-content-transform-origin": "var(--radix-popper-transform-origin)",
                  "--radix-popover-content-available-width": "var(--radix-popper-available-width)",
                  "--radix-popover-content-available-height": "var(--radix-popper-available-height)",
                  "--radix-popover-trigger-width": "var(--radix-popper-anchor-width)",
                  "--radix-popover-trigger-height": "var(--radix-popper-anchor-height)"
                }
              }
            )
          }
        )
      }
    );
  }
), I = "PopoverClose", to = c.forwardRef(
  (o, n) => {
    const { __scopePopover: t, ...a } = o, e = u(I, t);
    return /* @__PURE__ */ p(
      E.button,
      {
        type: "button",
        ...a,
        ref: n,
        onClick: P(o.onClick, () => e.onOpenChange(!1))
      }
    );
  }
);
to.displayName = I;
var no = "PopoverArrow", ao = c.forwardRef(
  (o, n) => {
    const { __scopePopover: t, ...a } = o, e = m(t);
    return /* @__PURE__ */ p(V, { ...e, ...a, ref: n });
  }
);
ao.displayName = no;
function T(o) {
  return o ? "open" : "closed";
}
var _o = F, xo = y, wo = D, Eo = M;
export {
  Eo as Content,
  F as Popover,
  Q as PopoverAnchor,
  ao as PopoverArrow,
  to as PopoverClose,
  M as PopoverContent,
  D as PopoverPortal,
  y as PopoverTrigger,
  wo as Portal,
  _o as Root,
  xo as Trigger
};
