"use client";
import { jsx as a, jsxs as ue } from "react/jsx-runtime";
import { cn as $ } from "../../../lib/utils.js";
import * as n from "react";
import { u as Te, a as Re, b as be, I as Se, F as Oe } from "../../../InternalBackdrop-BVEfe6O1.js";
import { u as Ie } from "../../../useOnFirstRender-B4aruaIA.js";
import { f as Y, a as xe, u as K } from "../../../useRenderElement-DNB2vtF1.js";
import * as J from "react-dom";
import { T as ye } from "../../../useTimeout-DAqPwigF.js";
import { u as Ne } from "../../../useOnMount-JHtE6teb.js";
import { R as Fe, P as Me, A as ke, d as R, o as we, p as De, q as le, u as Ve, B as _e, r as Ae, C as Be, E as He, j as Ue, G as Ge, k as Le, i as ce, z as Ke, s as ze, v as je, w as $e, x as de } from "../../../popupStateMapping-BT9Y_jIq.js";
import { p as Ye, c as qe, a as We, b as Je, u as Qe, d as Xe } from "../../../useSyncedFloatingRootContext-Bc90zSTy.js";
import { b as q, a as Q, e as Ze, f as X, c as Z, i as et } from "../../../createBaseUIEventDetails-CqidROtw.js";
import { u as tt } from "../../../useRole-BQ8R4yU3.js";
import { u as ee } from "../../../useStableCallback-CudqoENs.js";
import { a as ot } from "../../../useButton-JYPKQKj4.js";
import { u as te } from "../../../useBaseUiId-C8Dq0BQq.js";
import { u as nt, s as st, a as rt } from "../../../safePolygon-CJcmMc0u.js";
import { c as it } from "../../../element-DSo91iOi.js";
import { i as at } from "../../../inertValue-kpZZDBEy.js";
import { u as oe } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { u as ct, a as pt, g as fe } from "../../../getDisabledMountTransitionStyles-fmnaQYVj.js";
import { b as ut, a as lt, t as dt } from "../../../useOpenChangeComplete-DkeH9FGJ.js";
import { i as ft } from "../../../floating-ui.utils.dom-jr2suqy2.js";
import { C as gt } from "../../../composite-BSpUD7nI.js";
import { u as mt } from "../../../ToolbarRootContext-D7EsmJ5f.js";
const ne = /* @__PURE__ */ n.createContext(void 0);
process.env.NODE_ENV !== "production" && (ne.displayName = "PopoverRootContext");
function U(e) {
  const t = n.useContext(ne);
  if (t === void 0 && !e)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: PopoverRootContext is missing. Popover parts must be placed within <Popover.Root>." : Y(47));
  return t;
}
function vt() {
  return {
    ...qe(),
    disabled: !1,
    modal: !1,
    instantType: void 0,
    openMethod: null,
    openChangeReason: null,
    titleElementId: void 0,
    descriptionElementId: void 0,
    stickIfOpen: !0,
    nested: !1,
    openOnHover: !1,
    closeDelay: 0,
    hasViewport: !1
  };
}
const Pt = {
  ...Ye,
  disabled: R((e) => e.disabled),
  instantType: R((e) => e.instantType),
  openMethod: R((e) => e.openMethod),
  openChangeReason: R((e) => e.openChangeReason),
  modal: R((e) => e.modal),
  stickIfOpen: R((e) => e.stickIfOpen),
  titleElementId: R((e) => e.titleElementId),
  descriptionElementId: R((e) => e.descriptionElementId),
  openOnHover: R((e) => e.openOnHover),
  closeDelay: R((e) => e.closeDelay),
  hasViewport: R((e) => e.hasViewport)
};
class se extends Fe {
  constructor(t) {
    const o = {
      ...vt(),
      ...t
    };
    o.open && t?.mounted === void 0 && (o.mounted = !0), super(o, {
      popupRef: /* @__PURE__ */ n.createRef(),
      backdropRef: /* @__PURE__ */ n.createRef(),
      internalBackdropRef: /* @__PURE__ */ n.createRef(),
      onOpenChange: void 0,
      onOpenChangeComplete: void 0,
      triggerFocusTargetRef: /* @__PURE__ */ n.createRef(),
      beforeContentFocusGuardRef: /* @__PURE__ */ n.createRef(),
      stickIfOpenTimeout: new ye(),
      triggerElements: new Me()
    }, Pt);
  }
  setOpen = (t, o) => {
    const d = o.reason === q, l = o.reason === Q && o.event.detail === 0, c = !t && (o.reason === Ze || o.reason == null);
    if (o.preventUnmountOnClose = () => {
      this.set("preventUnmountingOnClose", !0);
    }, this.context.onOpenChange?.(t, o), o.isCanceled)
      return;
    const u = {
      open: t,
      nativeEvent: o.event,
      reason: o.reason,
      nested: this.state.nested,
      triggerElement: o.trigger
    };
    this.state.floatingRootContext.context.events?.emit("openchange", u);
    const s = () => {
      const v = {
        open: t,
        openChangeReason: o.reason
      }, r = o.trigger?.id ?? null;
      (r || t) && (v.activeTriggerId = r, v.activeTriggerElement = o.trigger ?? null), this.update(v);
    };
    d ? (this.set("stickIfOpen", !0), this.context.stickIfOpenTimeout.start(ke, () => {
      this.set("stickIfOpen", !1);
    }), J.flushSync(s)) : s(), l || c ? this.set("instantType", l ? "click" : "dismiss") : o.reason === X ? this.set("instantType", "focus") : this.set("instantType", void 0);
  };
  static useStore(t, o) {
    const d = xe(() => new se(o)).current, l = t ?? d;
    return Ne(d.disposeEffect), l;
  }
  disposeEffect = () => this.context.stickIfOpenTimeout.disposeEffect();
}
function pe({
  props: e
}) {
  const {
    children: t,
    open: o,
    defaultOpen: d = !1,
    onOpenChange: l,
    onOpenChangeComplete: c,
    modal: u = !1,
    handle: f,
    triggerId: s,
    defaultTriggerId: v = null
  } = e, r = se.useStore(f?.store, {
    modal: u,
    open: d,
    openProp: o,
    activeTriggerId: v,
    triggerIdProp: s
  });
  Ie(() => {
    o === void 0 && r.state.open === !1 && d === !0 && r.update({
      open: !0,
      activeTriggerId: v
    });
  }), r.useControlledProp("openProp", o), r.useControlledProp("triggerIdProp", s);
  const C = r.useState("open"), w = r.useState("positionerElement"), S = r.useState("payload"), D = r.useState("openChangeReason");
  r.useContextCallback("onOpenChange", l), r.useContextCallback("onOpenChangeComplete", c);
  const {
    openMethod: p,
    triggerProps: m
  } = Te(C);
  We(r);
  const {
    forceUnmount: O
  } = Je(C, r, () => {
    r.update({
      stickIfOpen: !0,
      openChangeReason: null
    });
  });
  Re(C && u === !0 && D !== q && p !== "touch", w), n.useEffect(() => {
    C || r.context.stickIfOpenTimeout.clear();
  }, [r, C]);
  const i = n.useCallback((H) => {
    const x = Z(H);
    return x.preventUnmountOnClose = () => {
      r.set("preventUnmountingOnClose", !0);
    }, x;
  }, [r]), y = n.useCallback(() => {
    r.setOpen(!1, i(et));
  }, [r, i]);
  n.useImperativeHandle(e.actionsRef, () => ({
    unmount: O,
    close: y
  }), [O, y]);
  const h = Qe({
    popupStore: r,
    onOpenChange: r.setOpen
  }), N = De(h, {
    outsidePressEvent: {
      // Ensure `aria-hidden` on outside elements is removed immediately
      // on outside press when trapping focus.
      mouse: u === "trap-focus" ? "sloppy" : "intentional",
      touch: "sloppy"
    }
  }), b = tt(h), {
    getReferenceProps: P,
    getFloatingProps: I,
    getTriggerProps: F
  } = le([N, b]), V = n.useMemo(() => P(m), [P, m]), _ = n.useMemo(() => F(m), [F, m]), A = n.useMemo(() => I(), [I]);
  r.useSyncedValues({
    modal: u,
    openMethod: p,
    activeTriggerProps: V,
    inactiveTriggerProps: _,
    popupProps: A,
    floatingRootContext: h,
    nested: Ve() != null
  });
  const B = n.useMemo(() => ({
    store: r
  }), [r]);
  return /* @__PURE__ */ a(ne.Provider, {
    value: B,
    children: typeof t == "function" ? t({
      payload: S
    }) : t
  });
}
function ht(e) {
  return U(!0) ? /* @__PURE__ */ a(pe, {
    props: e
  }) : /* @__PURE__ */ a(we, {
    children: /* @__PURE__ */ a(pe, {
      props: e
    })
  });
}
const Ct = 300, ge = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    render: d,
    className: l,
    disabled: c = !1,
    nativeButton: u = !0,
    handle: f,
    payload: s,
    openOnHover: v = !1,
    delay: r = Ct,
    closeDelay: C = 0,
    id: w,
    ...S
  } = t, D = U(!0), p = f?.store ?? D?.store;
  if (!p)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <Popover.Trigger> must be either used within a <Popover.Root> component or provided with a handle." : Y(74));
  const m = te(w), O = p.useState("isTriggerActive", m), i = p.useState("floatingRootContext"), y = p.useState("isOpenedByTrigger", m), h = n.useRef(null), {
    registerTrigger: N,
    isMountedByThisTrigger: b
  } = Xe(m, h, p, {
    payload: s,
    disabled: c,
    openOnHover: v,
    closeDelay: C
  }), P = p.useState("openChangeReason"), I = p.useState("stickIfOpen"), F = p.useState("openMethod"), V = nt(i, {
    enabled: i != null && v && (F !== "touch" || P !== Q),
    mouseOnly: !0,
    move: !1,
    handleClose: st(),
    restMs: r,
    delay: {
      close: C
    },
    triggerElementRef: h,
    isActiveTrigger: O
  }), _ = be(i, {
    enabled: i != null,
    stickIfOpen: I
  }), A = le([_]), B = p.useState("triggerProps", b), H = {
    disabled: c,
    open: y
  }, {
    getButtonProps: x,
    buttonRef: G
  } = ot({
    disabled: c,
    native: u
  }), M = n.useMemo(() => ({
    open(T) {
      return T && P === Q ? _e.open(T) : Ae.open(T);
    }
  }), [P]), E = K("button", t, {
    state: H,
    ref: [G, o, N, h],
    props: [A.getReferenceProps(), V, B, {
      [Be]: "",
      id: m
    }, S, x],
    stateAttributesMapping: M
  }), k = n.useRef(null), z = ee((T) => {
    J.flushSync(() => {
      p.setOpen(!1, Z(X, T.nativeEvent, T.currentTarget));
    }), He(k.current)?.focus();
  }), W = ee((T) => {
    const L = p.select("positionerElement");
    if (L && Ue(T, L))
      p.context.beforeContentFocusGuardRef.current?.focus();
    else {
      J.flushSync(() => {
        p.setOpen(!1, Z(X, T.nativeEvent, T.currentTarget));
      });
      let g = Ge(p.context.triggerFocusTargetRef.current || h.current);
      for (; g !== null && it(L, g); ) {
        const j = g;
        if (g = Le(g), g === j)
          break;
      }
      g?.focus();
    }
  });
  return O ? /* @__PURE__ */ ue(n.Fragment, {
    children: [/* @__PURE__ */ a(ce, {
      ref: k,
      onFocus: z
    }), /* @__PURE__ */ a(n.Fragment, {
      children: E
    }, m), /* @__PURE__ */ a(ce, {
      ref: p.context.triggerFocusTargetRef,
      onFocus: W
    })]
  }) : /* @__PURE__ */ a(n.Fragment, {
    children: E
  }, m);
});
process.env.NODE_ENV !== "production" && (ge.displayName = "PopoverTrigger");
const re = /* @__PURE__ */ n.createContext(void 0);
process.env.NODE_ENV !== "production" && (re.displayName = "PopoverPortalContext");
function Et() {
  const e = n.useContext(re);
  if (e === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <Popover.Portal> is missing." : Y(45));
  return e;
}
const me = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    keepMounted: d = !1,
    ...l
  } = t, {
    store: c
  } = U();
  return c.useState("mounted") || d ? /* @__PURE__ */ a(re.Provider, {
    value: d,
    children: /* @__PURE__ */ a(Ke, {
      ref: o,
      ...l
    })
  }) : null;
});
process.env.NODE_ENV !== "production" && (me.displayName = "PopoverPortal");
const ie = /* @__PURE__ */ n.createContext(void 0);
process.env.NODE_ENV !== "production" && (ie.displayName = "PopoverPositionerContext");
function Tt() {
  const e = n.useContext(ie);
  if (!e)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: PopoverPositionerContext is missing. PopoverPositioner parts must be placed within <Popover.Positioner>." : Y(46));
  return e;
}
const ve = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    render: d,
    className: l,
    anchor: c,
    positionMethod: u = "absolute",
    side: f = "bottom",
    align: s = "center",
    sideOffset: v = 0,
    alignOffset: r = 0,
    collisionBoundary: C = "clipping-ancestors",
    collisionPadding: w = 5,
    arrowPadding: S = 5,
    sticky: D = !1,
    disableAnchorTracking: p = !1,
    collisionAvoidance: m = ze,
    ...O
  } = t, {
    store: i
  } = U(), y = Et(), h = je(), N = i.useState("floatingRootContext"), b = i.useState("mounted"), P = i.useState("open"), I = i.useState("openChangeReason"), F = i.useState("activeTriggerElement"), V = i.useState("modal"), _ = i.useState("positionerElement"), A = i.useState("instantType"), B = i.useState("transitionStatus"), H = i.useState("hasViewport"), x = n.useRef(null), G = ut(_, !1, !1), M = ct({
    anchor: c,
    floatingRootContext: N,
    positionMethod: u,
    mounted: b,
    side: f,
    sideOffset: v,
    align: s,
    alignOffset: r,
    arrowPadding: S,
    collisionBoundary: C,
    collisionPadding: w,
    sticky: D,
    disableAnchorTracking: p,
    keepMounted: y,
    nodeId: h,
    collisionAvoidance: m,
    adaptiveOrigin: H ? pt : void 0
  }), E = n.useMemo(() => {
    const g = {};
    return P || (g.pointerEvents = "none"), {
      role: "presentation",
      hidden: !b,
      style: {
        ...M.positionerStyles,
        ...g
      }
    };
  }, [P, b, M.positionerStyles]), k = n.useMemo(() => ({
    props: E,
    ...M
  }), [E, M]), z = N.useState("domReferenceElement");
  oe(() => {
    const g = z, j = x.current;
    if (g && (x.current = g), j && g && g !== j) {
      i.set("instantType", void 0);
      const ae = new AbortController();
      return G(() => {
        i.set("instantType", "trigger-change");
      }, ae.signal), () => {
        ae.abort();
      };
    }
  }, [z, G, i]);
  const W = {
    open: P,
    side: k.side,
    align: k.align,
    anchorHidden: k.anchorHidden,
    instant: A
  }, T = n.useCallback((g) => {
    i.set("positionerElement", g);
  }, [i]), L = K("div", t, {
    state: W,
    props: [k.props, fe(B), O],
    ref: [o, T],
    stateAttributesMapping: de
  });
  return /* @__PURE__ */ ue(ie.Provider, {
    value: k,
    children: [b && V === !0 && I !== q && /* @__PURE__ */ a(Se, {
      ref: i.context.internalBackdropRef,
      inert: at(!P),
      cutout: F
    }), /* @__PURE__ */ a($e, {
      id: h,
      children: L
    })]
  });
});
process.env.NODE_ENV !== "production" && (ve.displayName = "PopoverPositioner");
const Pe = /* @__PURE__ */ n.createContext(void 0);
process.env.NODE_ENV !== "production" && (Pe.displayName = "ClosePartContext");
function Rt() {
  const [e, t] = n.useState(0), o = ee(() => (t((l) => l + 1), () => {
    t((l) => Math.max(0, l - 1));
  }));
  return {
    context: n.useMemo(() => ({
      register: o
    }), [o]),
    hasClosePart: e > 0
  };
}
function bt(e) {
  const {
    value: t,
    children: o
  } = e;
  return /* @__PURE__ */ a(Pe.Provider, {
    value: t,
    children: o
  });
}
const St = {
  ...de,
  ...dt
}, he = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    className: d,
    render: l,
    initialFocus: c,
    finalFocus: u,
    ...f
  } = t, {
    store: s
  } = U(), v = Tt(), r = mt() != null, {
    context: C,
    hasClosePart: w
  } = Rt(), S = s.useState("open"), D = s.useState("openMethod"), p = s.useState("instantType"), m = s.useState("transitionStatus"), O = s.useState("popupProps"), i = s.useState("titleElementId"), y = s.useState("descriptionElementId"), h = s.useState("modal"), N = s.useState("mounted"), b = s.useState("openChangeReason"), P = s.useState("activeTriggerElement"), I = s.useState("floatingRootContext");
  lt({
    open: S,
    ref: s.context.popupRef,
    onComplete() {
      S && s.context.onOpenChangeComplete?.(!0);
    }
  });
  const F = s.useState("disabled"), V = s.useState("openOnHover"), _ = s.useState("closeDelay");
  rt(I, {
    enabled: V && !F,
    closeDelay: _
  });
  function A(E) {
    return E === "touch" ? s.context.popupRef.current : !0;
  }
  const B = c === void 0 ? A : c, H = {
    open: S,
    side: v.side,
    align: v.align,
    instant: p,
    transitionStatus: m
  }, x = h !== !1 && w, G = n.useCallback((E) => {
    s.set("popupElement", E);
  }, [s]), M = K("div", t, {
    state: H,
    ref: [o, s.context.popupRef, G],
    props: [O, {
      "aria-labelledby": i,
      "aria-describedby": y,
      onKeyDown(E) {
        r && gt.has(E.key) && E.stopPropagation();
      }
    }, fe(m), f],
    stateAttributesMapping: St
  });
  return /* @__PURE__ */ a(Oe, {
    context: I,
    openInteractionType: D,
    modal: x,
    disabled: !N || b === q,
    initialFocus: B,
    returnFocus: u,
    restoreFocus: "popup",
    previousFocusableElement: ft(P) ? P : void 0,
    nextFocusableElement: s.context.triggerFocusTargetRef,
    beforeContentFocusGuardRef: s.context.beforeContentFocusGuardRef,
    children: /* @__PURE__ */ a(bt, {
      value: C,
      children: M
    })
  });
});
process.env.NODE_ENV !== "production" && (he.displayName = "PopoverPopup");
const Ce = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    render: d,
    className: l,
    ...c
  } = t, {
    store: u
  } = U(), f = te(c.id);
  return oe(() => (u.set("titleElementId", f), () => {
    u.set("titleElementId", void 0);
  }), [u, f]), K("h2", t, {
    ref: o,
    props: [{
      id: f
    }, c]
  });
});
process.env.NODE_ENV !== "production" && (Ce.displayName = "PopoverTitle");
const Ee = /* @__PURE__ */ n.forwardRef(function(t, o) {
  const {
    render: d,
    className: l,
    ...c
  } = t, {
    store: u
  } = U(), f = te(c.id);
  return oe(() => (u.set("descriptionElementId", f), () => {
    u.set("descriptionElementId", void 0);
  }), [u, f]), K("p", t, {
    ref: o,
    props: [{
      id: f
    }, c]
  });
});
process.env.NODE_ENV !== "production" && (Ee.displayName = "PopoverDescription");
function qt({ ...e }) {
  return /* @__PURE__ */ a(ht, { "data-slot": "popover", ...e });
}
function Wt({ ...e }) {
  return /* @__PURE__ */ a(ge, { "data-slot": "popover-trigger", ...e });
}
function Jt({
  className: e,
  align: t = "center",
  alignOffset: o = 0,
  side: d = "bottom",
  sideOffset: l = 4,
  ...c
}) {
  return /* @__PURE__ */ a(me, { children: /* @__PURE__ */ a(
    ve,
    {
      align: t,
      alignOffset: o,
      className: "isolate z-50",
      side: d,
      sideOffset: l,
      children: /* @__PURE__ */ a(
        he,
        {
          className: $(
            "data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:fade-in-0 data-open:zoom-in-95 data-closed:fade-out-0 data-closed:zoom-out-95 z-50 flex w-72 origin-(--transform-origin) flex-col gap-4 rounded-md bg-popover p-4 text-popover-foreground text-sm shadow-md outline-hidden ring-1 ring-foreground/10 duration-100 data-closed:animate-out data-open:animate-in",
            e
          ),
          "data-slot": "popover-content",
          ...c
        }
      )
    }
  ) });
}
function Qt({ className: e, ...t }) {
  return /* @__PURE__ */ a(
    "div",
    {
      className: $("flex flex-col gap-1 text-sm", e),
      "data-slot": "popover-header",
      ...t
    }
  );
}
function Xt({ className: e, ...t }) {
  return /* @__PURE__ */ a(
    Ce,
    {
      className: $("font-medium", e),
      "data-slot": "popover-title",
      ...t
    }
  );
}
function Zt({
  className: e,
  ...t
}) {
  return /* @__PURE__ */ a(
    Ee,
    {
      className: $("text-muted-foreground", e),
      "data-slot": "popover-description",
      ...t
    }
  );
}
export {
  qt as Popover,
  Jt as PopoverContent,
  Zt as PopoverDescription,
  Qt as PopoverHeader,
  Xt as PopoverTitle,
  Wt as PopoverTrigger
};
