"use client";
import { jsx as f } from "react/jsx-runtime";
import { cn as $ } from "../../../lib/utils.js";
import * as s from "react";
import { u as L } from "../../../useIsoLayoutEffect-DpJo20by.js";
import { u as ee } from "../../../useOnFirstRender-B4aruaIA.js";
import { f as N, a as te, u as b } from "../../../useRenderElement-DNB2vtF1.js";
import { b as H, t as oe, a as ne, e as re, c as se, i as ie } from "../../../createBaseUIEventDetails-CqidROtw.js";
import * as ae from "react-dom";
import { R as de, P as ce, d as B, o as ue, p as pe, q as le, r as ge, s as fe, v as me, w as Ce, x as z } from "../../../popupStateMapping-BT9Y_jIq.js";
import { p as ve, u as Pe, c as we, a as he, b as Se, d as ye } from "../../../useSyncedFloatingRootContext-Bc90zSTy.js";
import { F as Re } from "../../../FloatingPortalLite-BHsRx4XN.js";
import { u as Te, a as Oe, g as W } from "../../../getDisabledMountTransitionStyles-fmnaQYVj.js";
import { u as Ee } from "../../../useStableCallback-CudqoENs.js";
import { a as xe, t as Ne } from "../../../useOpenChangeComplete-DkeH9FGJ.js";
import { u as Ie, s as De, a as be } from "../../../safePolygon-CJcmMc0u.js";
import { u as Me } from "../../../useBaseUiId-C8Dq0BQq.js";
import { u as Ve } from "../../../useFocus-Aj9ShMQu.js";
const M = /* @__PURE__ */ s.createContext(void 0);
process.env.NODE_ENV !== "production" && (M.displayName = "PreviewCardRootContext");
function x(t) {
  const e = s.useContext(M);
  if (e === void 0 && !t)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: PreviewCardRootContext is missing. PreviewCard parts must be placed within <PreviewCard.Root>." : N(50));
  return e;
}
const Ae = 600, Y = 300, Ue = {
  ...ve,
  instantType: B((t) => t.instantType),
  hasViewport: B((t) => t.hasViewport)
};
class V extends de {
  constructor(e) {
    super({
      ..._e(),
      ...e
    }, {
      popupRef: /* @__PURE__ */ s.createRef(),
      onOpenChange: void 0,
      onOpenChangeComplete: void 0,
      triggerElements: new ce(),
      closeDelayRef: {
        current: Y
      }
    }, Ue);
  }
  setOpen = (e, n) => {
    const i = n.reason, a = i === H, p = e && i === oe, r = !e && (i === ne || i === re);
    if (n.preventUnmountOnClose = () => {
      this.set("preventUnmountingOnClose", !0);
    }, this.context.onOpenChange?.(e, n), n.isCanceled)
      return;
    const m = () => {
      const d = {
        open: e
      };
      p ? d.instantType = "focus" : r ? d.instantType = "dismiss" : i === H && (d.instantType = void 0);
      const l = n.trigger?.id ?? null;
      (l || e) && (d.activeTriggerId = l, d.activeTriggerElement = n.trigger ?? null), this.update(d);
    };
    a ? ae.flushSync(m) : m();
  };
  static useStore(e, n) {
    const i = te(() => new V(n)).current, a = e ?? i, p = Pe({
      popupStore: a,
      onOpenChange: a.setOpen
    });
    return a.state.floatingRootContext = p, a;
  }
}
function _e() {
  return {
    ...we(),
    instantType: void 0,
    hasViewport: !1
  };
}
function k(t) {
  const {
    open: e,
    defaultOpen: n = !1,
    onOpenChange: i,
    onOpenChangeComplete: a,
    actionsRef: p,
    handle: r,
    triggerId: m,
    defaultTriggerId: d = null,
    children: l
  } = t, o = V.useStore(r?.store, {
    open: n,
    openProp: e,
    activeTriggerId: d,
    triggerIdProp: m
  });
  ee(() => {
    e === void 0 && o.state.open === !1 && n === !0 && o.update({
      open: !0,
      activeTriggerId: d
    });
  }), o.useControlledProp("openProp", e), o.useControlledProp("triggerIdProp", m), o.useContextCallback("onOpenChange", i), o.useContextCallback("onOpenChangeComplete", a);
  const C = o.useState("open"), c = o.useState("activeTriggerId"), v = o.useState("payload");
  he(o);
  const {
    forceUnmount: w
  } = Se(C, o);
  L(() => {
    C && c == null && o.set("payload", void 0);
  }, [o, c, C]);
  const S = s.useCallback(() => {
    o.setOpen(!1, He(o, ie));
  }, [o]);
  s.useImperativeHandle(p, () => ({
    unmount: w,
    close: S
  }), [w, S]);
  const y = o.useState("floatingRootContext"), R = pe(y), {
    getReferenceProps: g,
    getTriggerProps: h,
    getFloatingProps: T
  } = le([R]), P = s.useMemo(() => g(), [g]), O = s.useMemo(() => h(), [h]), E = s.useMemo(() => T(), [T]);
  return o.useSyncedValues({
    activeTriggerProps: P,
    inactiveTriggerProps: O,
    popupProps: E
  }), /* @__PURE__ */ f(M.Provider, {
    value: o,
    children: typeof l == "function" ? l({
      payload: v
    }) : l
  });
}
function Fe(t) {
  return x(!0) ? /* @__PURE__ */ f(k, {
    ...t
  }) : /* @__PURE__ */ f(ue, {
    children: /* @__PURE__ */ f(k, {
      ...t
    })
  });
}
function He(t, e) {
  const n = se(e);
  return n.preventUnmountOnClose = () => {
    t.set("preventUnmountingOnClose", !0);
  }, n;
}
const A = /* @__PURE__ */ s.createContext(void 0);
process.env.NODE_ENV !== "production" && (A.displayName = "PreviewCardPortalContext");
function Be() {
  const t = s.useContext(A);
  if (t === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <PreviewCard.Portal> is missing." : N(48));
  return t;
}
const j = /* @__PURE__ */ s.forwardRef(function(e, n) {
  const {
    keepMounted: i = !1,
    ...a
  } = e;
  return x().useState("mounted") || i ? /* @__PURE__ */ f(A.Provider, {
    value: i,
    children: /* @__PURE__ */ f(Re, {
      ref: n,
      ...a
    })
  }) : null;
});
process.env.NODE_ENV !== "production" && (j.displayName = "PreviewCardPortal");
const q = /* @__PURE__ */ s.forwardRef(function(e, n) {
  const {
    render: i,
    className: a,
    delay: p,
    closeDelay: r,
    id: m,
    payload: d,
    handle: l,
    ...o
  } = e, C = x(!0), c = l?.store ?? C;
  if (!c)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <PreviewCard.Trigger> must be either used within a <PreviewCard.Root> component or provided with a handle." : N(89));
  const v = Me(m), w = c.useState("isTriggerActive", v), S = c.useState("isOpenedByTrigger", v), y = c.useState("floatingRootContext"), R = s.useRef(null), g = p ?? Ae, h = r ?? Y, {
    registerTrigger: T,
    isMountedByThisTrigger: P
  } = ye(v, R, c, {
    payload: d
  });
  L(() => {
    P && (c.context.closeDelayRef.current = h);
  }, [c, P, h]);
  const O = Ie(y, {
    mouseOnly: !0,
    move: !1,
    handleClose: De(),
    delay: () => ({
      open: g,
      close: h
    }),
    triggerElementRef: R,
    isActiveTrigger: w
  }), E = Ve(y, {
    delay: g
  }), I = {
    open: S
  }, D = c.useState("triggerProps", P);
  return b("a", e, {
    state: I,
    ref: [n, T, R],
    props: [O, E.reference, D, {
      id: v
    }, o],
    stateAttributesMapping: ge
  });
});
process.env.NODE_ENV !== "production" && (q.displayName = "PreviewCardTrigger");
const U = /* @__PURE__ */ s.createContext(void 0);
process.env.NODE_ENV !== "production" && (U.displayName = "PreviewCardPositionerContext");
function ke() {
  const t = s.useContext(U);
  if (t === void 0)
    throw new Error(process.env.NODE_ENV !== "production" ? "Base UI: <PreviewCard.Popup> and <PreviewCard.Arrow> must be used within the <PreviewCard.Positioner> component" : N(49));
  return t;
}
const K = /* @__PURE__ */ s.forwardRef(function(e, n) {
  const {
    render: i,
    className: a,
    anchor: p,
    positionMethod: r = "absolute",
    side: m = "bottom",
    align: d = "center",
    sideOffset: l = 0,
    alignOffset: o = 0,
    collisionBoundary: C = "clipping-ancestors",
    collisionPadding: c = 5,
    arrowPadding: v = 5,
    sticky: w = !1,
    disableAnchorTracking: S = !1,
    collisionAvoidance: y = fe,
    ...R
  } = e, g = x(), h = Be(), T = me(), P = g.useState("open"), O = g.useState("mounted"), E = g.useState("floatingRootContext"), I = g.useState("instantType"), D = g.useState("transitionStatus"), _ = g.useState("hasViewport"), u = Te({
    anchor: p,
    floatingRootContext: E,
    positionMethod: r,
    mounted: O,
    side: m,
    sideOffset: l,
    align: d,
    alignOffset: o,
    arrowPadding: v,
    collisionBoundary: C,
    collisionPadding: c,
    sticky: w,
    disableAnchorTracking: S,
    keepMounted: h,
    nodeId: T,
    collisionAvoidance: y,
    adaptiveOrigin: _ ? Oe : void 0
  }), J = s.useMemo(() => {
    const F = {};
    return P || (F.pointerEvents = "none"), {
      role: "presentation",
      hidden: !O,
      style: {
        ...u.positionerStyles,
        ...F
      }
    };
  }, [P, O, u.positionerStyles]), Q = {
    open: P,
    side: u.side,
    align: u.align,
    anchorHidden: u.anchorHidden,
    instant: I
  }, X = s.useMemo(() => ({
    side: u.side,
    align: u.align,
    arrowRef: u.arrowRef,
    arrowUncentered: u.arrowUncentered,
    arrowStyles: u.arrowStyles
  }), [u.side, u.align, u.arrowRef, u.arrowUncentered, u.arrowStyles]), Z = b("div", e, {
    state: Q,
    props: [J, W(D), R],
    ref: [n, g.useStateSetter("positionerElement")],
    stateAttributesMapping: z
  });
  return /* @__PURE__ */ f(U.Provider, {
    value: X,
    children: /* @__PURE__ */ f(Ce, {
      id: T,
      children: Z
    })
  });
});
process.env.NODE_ENV !== "production" && (K.displayName = "PreviewCardPositioner");
const Le = {
  ...z,
  ...Ne
}, G = /* @__PURE__ */ s.forwardRef(function(e, n) {
  const {
    className: i,
    render: a,
    ...p
  } = e, r = x(), {
    side: m,
    align: d
  } = ke(), l = r.useState("open"), o = r.useState("instantType"), C = r.useState("transitionStatus"), c = r.useState("popupProps"), v = r.useState("floatingRootContext");
  xe({
    open: l,
    ref: r.context.popupRef,
    onComplete() {
      l && r.context.onOpenChangeComplete?.(!0);
    }
  });
  const w = Ee(() => r.context.closeDelayRef.current);
  return be(v, {
    closeDelay: w
  }), b("div", e, {
    state: {
      open: l,
      side: m,
      align: d,
      instant: o,
      transitionStatus: C
    },
    ref: [n, r.context.popupRef, r.useStateSetter("popupElement")],
    props: [c, W(C), p],
    stateAttributesMapping: Le
  });
});
process.env.NODE_ENV !== "production" && (G.displayName = "PreviewCardPopup");
function nt({ ...t }) {
  return /* @__PURE__ */ f(Fe, { "data-slot": "hover-card", ...t });
}
function rt({ ...t }) {
  return /* @__PURE__ */ f(q, { "data-slot": "hover-card-trigger", ...t });
}
function st({
  className: t,
  side: e = "bottom",
  sideOffset: n = 4,
  align: i = "center",
  alignOffset: a = 4,
  ...p
}) {
  return /* @__PURE__ */ f(j, { "data-slot": "hover-card-portal", children: /* @__PURE__ */ f(
    K,
    {
      align: i,
      alignOffset: a,
      className: "isolate z-50",
      side: e,
      sideOffset: n,
      children: /* @__PURE__ */ f(
        G,
        {
          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 w-64 origin-(--transform-origin) rounded-lg 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",
            t
          ),
          "data-slot": "hover-card-content",
          ...p
        }
      )
    }
  ) });
}
export {
  nt as HoverCard,
  st as HoverCardContent,
  rt as HoverCardTrigger
};
