import * as s from "react";
import { composeEventHandlers as g } from "../../../../../@radix-ui_primitive@1.1.0/node_modules/@radix-ui/primitive/dist/index.js";
import { useComposedRefs as H } from "../../../../../@radix-ui_react-compose-refs@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-compose-refs/dist/index.js";
import { createContextScope as W } from "../../../../../@radix-ui_react-context@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-context/dist/index.js";
import { DismissableLayer as $ } from "../../../../../@radix-ui_react-dismissable-layer@1.1.0_@types_react-dom@18.3.6_@types_react@18.3.3__@t_540819d22b5f34e77f512765c1a78ce5/node_modules/@radix-ui/react-dismissable-layer/dist/index.js";
import { useId as z } from "../../../../../@radix-ui_react-id@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-id/dist/index.js";
import { createPopperScope as N, Root as J, Anchor as Q, Arrow as Z, Content as ee } from "../../../../../@radix-ui_react-popper@1.2.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@_0c236d22233b5684ecf8a3d8d1b24562/node_modules/@radix-ui/react-popper/dist/index.js";
import { Portal as te } from "../../../../../@radix-ui_react-portal@1.1.1_@types_react-dom@18.3.6_@types_react@18.3.3__@types_react@_d91fd04d10d265136bf0d35a7b9375b2/node_modules/@radix-ui/react-portal/dist/index.js";
import { Presence as S } from "../../../../../@radix-ui_react-presence@1.1.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_reac_4a916339a724b4591202705df11dd5fc/node_modules/@radix-ui/react-presence/dist/index.js";
import { Primitive as oe } from "../../../../../@radix-ui_react-primitive@2.0.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_rea_bdcf182289a3a76356f07e550726e479/node_modules/@radix-ui/react-primitive/dist/index.js";
import { Slottable as re } from "../../../../../@radix-ui_react-slot@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-slot/dist/index.js";
import { useControllableState as ne } from "../../../../../@radix-ui_react-use-controllable-state@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-controllable-state/dist/index.js";
import { Root as se } from "../../../../../@radix-ui_react-visually-hidden@1.1.0_@types_react-dom@18.3.6_@types_react@18.3.3__@typ_ed6b83029f4b3a260b4f0503b9e9f52c/node_modules/@radix-ui/react-visually-hidden/dist/index.js";
import { j as u } from "../../../../../../../_virtual/jsx-runtime.js";
var [_, ke] = W("Tooltip", [
  N
]), O = N(), G = "TooltipProvider", ae = 700, A = "tooltip.open", [ie, j] = _(G), F = (t) => {
  const {
    __scopeTooltip: o,
    delayDuration: e = ae,
    skipDelayDuration: r = 300,
    disableHoverableContent: n = !1,
    children: i
  } = t, [l, f] = s.useState(!0), a = s.useRef(!1), p = s.useRef(0);
  return s.useEffect(() => {
    const c = p.current;
    return () => window.clearTimeout(c);
  }, []), /* @__PURE__ */ u.jsx(
    ie,
    {
      scope: o,
      isOpenDelayed: l,
      delayDuration: e,
      onOpen: s.useCallback(() => {
        window.clearTimeout(p.current), f(!1);
      }, []),
      onClose: s.useCallback(() => {
        window.clearTimeout(p.current), p.current = window.setTimeout(
          () => f(!0),
          r
        );
      }, [r]),
      isPointerInTransitRef: a,
      onPointerInTransitChange: s.useCallback((c) => {
        a.current = c;
      }, []),
      disableHoverableContent: n,
      children: i
    }
  );
};
F.displayName = G;
var D = "Tooltip", [le, R] = _(D), B = (t) => {
  const {
    __scopeTooltip: o,
    children: e,
    open: r,
    defaultOpen: n = !1,
    onOpenChange: i,
    disableHoverableContent: l,
    delayDuration: f
  } = t, a = j(D, t.__scopeTooltip), p = O(o), [c, d] = s.useState(null), h = z(), m = s.useRef(0), v = l ?? a.disableHoverableContent, T = f ?? a.delayDuration, x = s.useRef(!1), [C = !1, y] = ne({
    prop: r,
    defaultProp: n,
    onChange: (M) => {
      M ? (a.onOpen(), document.dispatchEvent(new CustomEvent(A))) : a.onClose(), i?.(M);
    }
  }), w = s.useMemo(() => C ? x.current ? "delayed-open" : "instant-open" : "closed", [C]), P = s.useCallback(() => {
    window.clearTimeout(m.current), x.current = !1, y(!0);
  }, [y]), E = s.useCallback(() => {
    window.clearTimeout(m.current), y(!1);
  }, [y]), I = s.useCallback(() => {
    window.clearTimeout(m.current), m.current = window.setTimeout(() => {
      x.current = !0, y(!0);
    }, T);
  }, [T, y]);
  return s.useEffect(() => () => window.clearTimeout(m.current), []), /* @__PURE__ */ u.jsx(J, { ...p, children: /* @__PURE__ */ u.jsx(
    le,
    {
      scope: o,
      contentId: h,
      open: C,
      stateAttribute: w,
      trigger: c,
      onTriggerChange: d,
      onTriggerEnter: s.useCallback(() => {
        a.isOpenDelayed ? I() : P();
      }, [a.isOpenDelayed, I, P]),
      onTriggerLeave: s.useCallback(() => {
        v ? E() : window.clearTimeout(m.current);
      }, [E, v]),
      onOpen: P,
      onClose: E,
      disableHoverableContent: v,
      children: e
    }
  ) });
};
B.displayName = D;
var L = "TooltipTrigger", U = s.forwardRef(
  (t, o) => {
    const { __scopeTooltip: e, ...r } = t, n = R(L, e), i = j(L, e), l = O(e), f = s.useRef(null), a = H(o, f, n.onTriggerChange), p = s.useRef(!1), c = s.useRef(!1), d = s.useCallback(() => p.current = !1, []);
    return s.useEffect(() => () => document.removeEventListener("pointerup", d), [d]), /* @__PURE__ */ u.jsx(Q, { asChild: !0, ...l, children: /* @__PURE__ */ u.jsx(
      oe.button,
      {
        "aria-describedby": n.open ? n.contentId : void 0,
        "data-state": n.stateAttribute,
        ...r,
        ref: a,
        onPointerMove: g(t.onPointerMove, (h) => {
          h.pointerType !== "touch" && !c.current && !i.isPointerInTransitRef.current && (n.onTriggerEnter(), c.current = !0);
        }),
        onPointerLeave: g(t.onPointerLeave, () => {
          n.onTriggerLeave(), c.current = !1;
        }),
        onPointerDown: g(t.onPointerDown, () => {
          p.current = !0, document.addEventListener("pointerup", d, { once: !0 });
        }),
        onFocus: g(t.onFocus, () => {
          p.current || n.onOpen();
        }),
        onBlur: g(t.onBlur, n.onClose),
        onClick: g(t.onClick, n.onClose)
      }
    ) });
  }
);
U.displayName = L;
var k = "TooltipPortal", [ce, ue] = _(k, {
  forceMount: void 0
}), V = (t) => {
  const { __scopeTooltip: o, forceMount: e, children: r, container: n } = t, i = R(k, o);
  return /* @__PURE__ */ u.jsx(ce, { scope: o, forceMount: e, children: /* @__PURE__ */ u.jsx(S, { present: e || i.open, children: /* @__PURE__ */ u.jsx(te, { asChild: !0, container: n, children: r }) }) });
};
V.displayName = k;
var b = "TooltipContent", Y = s.forwardRef(
  (t, o) => {
    const e = ue(b, t.__scopeTooltip), { forceMount: r = e.forceMount, side: n = "top", ...i } = t, l = R(b, t.__scopeTooltip);
    return /* @__PURE__ */ u.jsx(S, { present: r || l.open, children: l.disableHoverableContent ? /* @__PURE__ */ u.jsx(q, { side: n, ...i, ref: o }) : /* @__PURE__ */ u.jsx(pe, { side: n, ...i, ref: o }) });
  }
), pe = s.forwardRef((t, o) => {
  const e = R(b, t.__scopeTooltip), r = j(b, t.__scopeTooltip), n = s.useRef(null), i = H(o, n), [l, f] = s.useState(null), { trigger: a, onClose: p } = e, c = n.current, { onPointerInTransitChange: d } = r, h = s.useCallback(() => {
    f(null), d(!1);
  }, [d]), m = s.useCallback(
    (v, T) => {
      const x = v.currentTarget, C = { x: v.clientX, y: v.clientY }, y = ve(C, x.getBoundingClientRect()), w = he(C, y), P = xe(T.getBoundingClientRect()), E = Te([...w, ...P]);
      f(E), d(!0);
    },
    [d]
  );
  return s.useEffect(() => () => h(), [h]), s.useEffect(() => {
    if (a && c) {
      const v = (x) => m(x, c), T = (x) => m(x, a);
      return a.addEventListener("pointerleave", v), c.addEventListener("pointerleave", T), () => {
        a.removeEventListener("pointerleave", v), c.removeEventListener("pointerleave", T);
      };
    }
  }, [a, c, m, h]), s.useEffect(() => {
    if (l) {
      const v = (T) => {
        const x = T.target, C = { x: T.clientX, y: T.clientY }, y = a?.contains(x) || c?.contains(x), w = !me(C, l);
        y ? h() : w && (h(), p());
      };
      return document.addEventListener("pointermove", v), () => document.removeEventListener("pointermove", v);
    }
  }, [a, c, l, p, h]), /* @__PURE__ */ u.jsx(q, { ...t, ref: i });
}), [de, fe] = _(D, { isInside: !1 }), q = s.forwardRef(
  (t, o) => {
    const {
      __scopeTooltip: e,
      children: r,
      "aria-label": n,
      onEscapeKeyDown: i,
      onPointerDownOutside: l,
      ...f
    } = t, a = R(b, e), p = O(e), { onClose: c } = a;
    return s.useEffect(() => (document.addEventListener(A, c), () => document.removeEventListener(A, c)), [c]), s.useEffect(() => {
      if (a.trigger) {
        const d = (h) => {
          h.target?.contains(a.trigger) && c();
        };
        return window.addEventListener("scroll", d, { capture: !0 }), () => window.removeEventListener("scroll", d, { capture: !0 });
      }
    }, [a.trigger, c]), /* @__PURE__ */ u.jsx(
      $,
      {
        asChild: !0,
        disableOutsidePointerEvents: !1,
        onEscapeKeyDown: i,
        onPointerDownOutside: l,
        onFocusOutside: (d) => d.preventDefault(),
        onDismiss: c,
        children: /* @__PURE__ */ u.jsxs(
          ee,
          {
            "data-state": a.stateAttribute,
            ...p,
            ...f,
            ref: o,
            style: {
              ...f.style,
              "--radix-tooltip-content-transform-origin": "var(--radix-popper-transform-origin)",
              "--radix-tooltip-content-available-width": "var(--radix-popper-available-width)",
              "--radix-tooltip-content-available-height": "var(--radix-popper-available-height)",
              "--radix-tooltip-trigger-width": "var(--radix-popper-anchor-width)",
              "--radix-tooltip-trigger-height": "var(--radix-popper-anchor-height)"
            },
            children: [
              /* @__PURE__ */ u.jsx(re, { children: r }),
              /* @__PURE__ */ u.jsx(de, { scope: e, isInside: !0, children: /* @__PURE__ */ u.jsx(se, { id: a.contentId, role: "tooltip", children: n || r }) })
            ]
          }
        )
      }
    );
  }
);
Y.displayName = b;
var X = "TooltipArrow", K = s.forwardRef(
  (t, o) => {
    const { __scopeTooltip: e, ...r } = t, n = O(e);
    return fe(
      X,
      e
    ).isInside ? null : /* @__PURE__ */ u.jsx(Z, { ...n, ...r, ref: o });
  }
);
K.displayName = X;
function ve(t, o) {
  const e = Math.abs(o.top - t.y), r = Math.abs(o.bottom - t.y), n = Math.abs(o.right - t.x), i = Math.abs(o.left - t.x);
  switch (Math.min(e, r, n, i)) {
    case i:
      return "left";
    case n:
      return "right";
    case e:
      return "top";
    case r:
      return "bottom";
    default:
      throw new Error("unreachable");
  }
}
function he(t, o, e = 5) {
  const r = [];
  switch (o) {
    case "top":
      r.push(
        { x: t.x - e, y: t.y + e },
        { x: t.x + e, y: t.y + e }
      );
      break;
    case "bottom":
      r.push(
        { x: t.x - e, y: t.y - e },
        { x: t.x + e, y: t.y - e }
      );
      break;
    case "left":
      r.push(
        { x: t.x + e, y: t.y - e },
        { x: t.x + e, y: t.y + e }
      );
      break;
    case "right":
      r.push(
        { x: t.x - e, y: t.y - e },
        { x: t.x - e, y: t.y + e }
      );
      break;
  }
  return r;
}
function xe(t) {
  const { top: o, right: e, bottom: r, left: n } = t;
  return [
    { x: n, y: o },
    { x: e, y: o },
    { x: e, y: r },
    { x: n, y: r }
  ];
}
function me(t, o) {
  const { x: e, y: r } = t;
  let n = !1;
  for (let i = 0, l = o.length - 1; i < o.length; l = i++) {
    const f = o[i].x, a = o[i].y, p = o[l].x, c = o[l].y;
    a > r != c > r && e < (p - f) * (r - a) / (c - a) + f && (n = !n);
  }
  return n;
}
function Te(t) {
  const o = t.slice();
  return o.sort((e, r) => e.x < r.x ? -1 : e.x > r.x ? 1 : e.y < r.y ? -1 : e.y > r.y ? 1 : 0), ye(o);
}
function ye(t) {
  if (t.length <= 1) return t.slice();
  const o = [];
  for (let r = 0; r < t.length; r++) {
    const n = t[r];
    for (; o.length >= 2; ) {
      const i = o[o.length - 1], l = o[o.length - 2];
      if ((i.x - l.x) * (n.y - l.y) >= (i.y - l.y) * (n.x - l.x)) o.pop();
      else break;
    }
    o.push(n);
  }
  o.pop();
  const e = [];
  for (let r = t.length - 1; r >= 0; r--) {
    const n = t[r];
    for (; e.length >= 2; ) {
      const i = e[e.length - 1], l = e[e.length - 2];
      if ((i.x - l.x) * (n.y - l.y) >= (i.y - l.y) * (n.x - l.x)) e.pop();
      else break;
    }
    e.push(n);
  }
  return e.pop(), o.length === 1 && e.length === 1 && o[0].x === e[0].x && o[0].y === e[0].y ? o : o.concat(e);
}
var Ie = F, Me = B, He = U, Ne = V, Se = Y, Ge = K;
export {
  Ge as Arrow,
  Se as Content,
  Ne as Portal,
  Ie as Provider,
  Me as Root,
  B as Tooltip,
  K as TooltipArrow,
  Y as TooltipContent,
  V as TooltipPortal,
  F as TooltipProvider,
  U as TooltipTrigger,
  He as Trigger,
  ke as createTooltipScope
};
//# sourceMappingURL=index.js.map
