import w from "../../../@babel/runtime/helpers/esm/extends.js";
import { forwardRef as O, createElement as f, useRef as g, useState as A, useCallback as h, useEffect as y, useMemo as K } from "react";
import { composeEventHandlers as C } from "../../primitive/dist/index.js";
import { useComposedRefs as F } from "../../react-compose-refs/dist/index.js";
import { createContextScope as U } from "../../react-context/dist/index.js";
import { DismissableLayer as V } from "../../react-dismissable-layer/dist/index.js";
import { useId as Y } from "../../react-id/dist/index.js";
import { createPopperScope as B, Content as q, Root as X, Anchor as z } from "../../react-popper/dist/index.js";
import { Presence as J } from "../../react-presence/dist/index.js";
import { Primitive as Q } from "../../react-primitive/dist/index.js";
import { Slottable as W } from "../../react-slot/dist/index.js";
import { useControllableState as Z } from "../../react-use-controllable-state/dist/index.js";
import { Root as ee } from "../../react-visually-hidden/dist/index.js";
const [R, Ie] = U("Tooltip", [
  B
]), M = B(), te = "TooltipProvider", oe = 700, I = "tooltip.open", [ne, H] = R(te), re = (t) => {
  const { __scopeTooltip: o, delayDuration: e = oe, skipDelayDuration: n = 300, disableHoverableContent: r = !1, children: i } = t, [a, l] = A(!0), c = g(!1), u = g(0);
  return y(() => {
    const s = u.current;
    return () => window.clearTimeout(s);
  }, []), /* @__PURE__ */ f(ne, {
    scope: o,
    isOpenDelayed: a,
    delayDuration: e,
    onOpen: h(() => {
      window.clearTimeout(u.current), l(!1);
    }, []),
    onClose: h(() => {
      window.clearTimeout(u.current), u.current = window.setTimeout(
        () => l(!0),
        n
      );
    }, [
      n
    ]),
    isPointerInTransitRef: c,
    onPointerInTransitChange: h((s) => {
      c.current = s;
    }, []),
    disableHoverableContent: r
  }, i);
}, k = "Tooltip", [ce, L] = R(k), ae = (t) => {
  const { __scopeTooltip: o, children: e, open: n, defaultOpen: r = !1, onOpenChange: i, disableHoverableContent: a, delayDuration: l } = t, c = H(k, t.__scopeTooltip), u = M(o), [s, p] = A(null), v = Y(), d = g(0), $ = a ?? c.disableHoverableContent, x = l ?? c.delayDuration, b = g(!1), [T = !1, m] = Z({
    prop: n,
    defaultProp: r,
    onChange: (N) => {
      N ? (c.onOpen(), document.dispatchEvent(new CustomEvent(I))) : c.onClose(), i == null || i(N);
    }
  }), P = K(() => T ? b.current ? "delayed-open" : "instant-open" : "closed", [
    T
  ]), E = h(() => {
    window.clearTimeout(d.current), b.current = !1, m(!0);
  }, [
    m
  ]), D = h(() => {
    window.clearTimeout(d.current), m(!1);
  }, [
    m
  ]), S = h(() => {
    window.clearTimeout(d.current), d.current = window.setTimeout(() => {
      b.current = !0, m(!0);
    }, x);
  }, [
    x,
    m
  ]);
  return y(() => () => window.clearTimeout(d.current), []), /* @__PURE__ */ f(X, u, /* @__PURE__ */ f(ce, {
    scope: o,
    contentId: v,
    open: T,
    stateAttribute: P,
    trigger: s,
    onTriggerChange: p,
    onTriggerEnter: h(() => {
      c.isOpenDelayed ? S() : E();
    }, [
      c.isOpenDelayed,
      S,
      E
    ]),
    onTriggerLeave: h(() => {
      $ ? D() : window.clearTimeout(d.current);
    }, [
      D,
      $
    ]),
    onOpen: E,
    onClose: D,
    disableHoverableContent: $
  }, e));
}, G = "TooltipTrigger", se = /* @__PURE__ */ O((t, o) => {
  const { __scopeTooltip: e, ...n } = t, r = L(G, e), i = H(G, e), a = M(e), l = g(null), c = F(o, l, r.onTriggerChange), u = g(!1), s = g(!1), p = h(
    () => u.current = !1,
    []
  );
  return y(() => () => document.removeEventListener("pointerup", p), [
    p
  ]), /* @__PURE__ */ f(z, w({
    asChild: !0
  }, a), /* @__PURE__ */ f(Q.button, w({
    // We purposefully avoid adding `type=button` here because tooltip triggers are also
    // commonly anchors and the anchor `type` attribute signifies MIME type.
    "aria-describedby": r.open ? r.contentId : void 0,
    "data-state": r.stateAttribute
  }, n, {
    ref: c,
    onPointerMove: C(t.onPointerMove, (v) => {
      v.pointerType !== "touch" && !s.current && !i.isPointerInTransitRef.current && (r.onTriggerEnter(), s.current = !0);
    }),
    onPointerLeave: C(t.onPointerLeave, () => {
      r.onTriggerLeave(), s.current = !1;
    }),
    onPointerDown: C(t.onPointerDown, () => {
      u.current = !0, document.addEventListener("pointerup", p, {
        once: !0
      });
    }),
    onFocus: C(t.onFocus, () => {
      u.current || r.onOpen();
    }),
    onBlur: C(t.onBlur, r.onClose),
    onClick: C(t.onClick, r.onClose)
  })));
}), ie = "TooltipPortal", [Ae, le] = R(ie, {
  forceMount: void 0
}), _ = "TooltipContent", ue = /* @__PURE__ */ O((t, o) => {
  const e = le(_, t.__scopeTooltip), { forceMount: n = e.forceMount, side: r = "top", ...i } = t, a = L(_, t.__scopeTooltip);
  return /* @__PURE__ */ f(J, {
    present: n || a.open
  }, a.disableHoverableContent ? /* @__PURE__ */ f(j, w({
    side: r
  }, i, {
    ref: o
  })) : /* @__PURE__ */ f(pe, w({
    side: r
  }, i, {
    ref: o
  })));
}), pe = /* @__PURE__ */ O((t, o) => {
  const e = L(_, t.__scopeTooltip), n = H(_, t.__scopeTooltip), r = g(null), i = F(o, r), [a, l] = A(null), { trigger: c, onClose: u } = e, s = r.current, { onPointerInTransitChange: p } = n, v = h(() => {
    l(null), p(!1);
  }, [
    p
  ]), d = h(($, x) => {
    const b = $.currentTarget, T = {
      x: $.clientX,
      y: $.clientY
    }, m = fe(T, b.getBoundingClientRect()), P = $e(T, m), E = ve(x.getBoundingClientRect()), D = xe([
      ...P,
      ...E
    ]);
    l(D), p(!0);
  }, [
    p
  ]);
  return y(() => () => v(), [
    v
  ]), y(() => {
    if (c && s) {
      const $ = (b) => d(b, s), x = (b) => d(b, c);
      return c.addEventListener("pointerleave", $), s.addEventListener("pointerleave", x), () => {
        c.removeEventListener("pointerleave", $), s.removeEventListener("pointerleave", x);
      };
    }
  }, [
    c,
    s,
    d,
    v
  ]), y(() => {
    if (a) {
      const $ = (x) => {
        const b = x.target, T = {
          x: x.clientX,
          y: x.clientY
        }, m = (c == null ? void 0 : c.contains(b)) || (s == null ? void 0 : s.contains(b)), P = !be(T, a);
        m ? v() : P && (v(), u());
      };
      return document.addEventListener("pointermove", $), () => document.removeEventListener("pointermove", $);
    }
  }, [
    c,
    s,
    a,
    u,
    v
  ]), /* @__PURE__ */ f(j, w({}, t, {
    ref: i
  }));
}), [de, Me] = R(k, {
  isInside: !1
}), j = /* @__PURE__ */ O((t, o) => {
  const { __scopeTooltip: e, children: n, "aria-label": r, onEscapeKeyDown: i, onPointerDownOutside: a, ...l } = t, c = L(_, e), u = M(e), { onClose: s } = c;
  return y(() => (document.addEventListener(I, s), () => document.removeEventListener(I, s)), [
    s
  ]), y(() => {
    if (c.trigger) {
      const p = (v) => {
        const d = v.target;
        d != null && d.contains(c.trigger) && s();
      };
      return window.addEventListener("scroll", p, {
        capture: !0
      }), () => window.removeEventListener("scroll", p, {
        capture: !0
      });
    }
  }, [
    c.trigger,
    s
  ]), /* @__PURE__ */ f(V, {
    asChild: !0,
    disableOutsidePointerEvents: !1,
    onEscapeKeyDown: i,
    onPointerDownOutside: a,
    onFocusOutside: (p) => p.preventDefault(),
    onDismiss: s
  }, /* @__PURE__ */ f(q, w({
    "data-state": c.stateAttribute
  }, u, l, {
    ref: o,
    style: {
      ...l.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)"
    }
  }), /* @__PURE__ */ f(W, null, n), /* @__PURE__ */ f(de, {
    scope: e,
    isInside: !0
  }, /* @__PURE__ */ f(ee, {
    id: c.contentId,
    role: "tooltip"
  }, r || n))));
});
function fe(t, o) {
  const e = Math.abs(o.top - t.y), n = Math.abs(o.bottom - t.y), r = Math.abs(o.right - t.x), i = Math.abs(o.left - t.x);
  switch (Math.min(e, n, r, i)) {
    case i:
      return "left";
    case r:
      return "right";
    case e:
      return "top";
    case n:
      return "bottom";
    default:
      throw new Error("unreachable");
  }
}
function $e(t, o, e = 5) {
  const n = [];
  switch (o) {
    case "top":
      n.push({
        x: t.x - e,
        y: t.y + e
      }, {
        x: t.x + e,
        y: t.y + e
      });
      break;
    case "bottom":
      n.push({
        x: t.x - e,
        y: t.y - e
      }, {
        x: t.x + e,
        y: t.y - e
      });
      break;
    case "left":
      n.push({
        x: t.x + e,
        y: t.y - e
      }, {
        x: t.x + e,
        y: t.y + e
      });
      break;
    case "right":
      n.push({
        x: t.x - e,
        y: t.y - e
      }, {
        x: t.x - e,
        y: t.y + e
      });
      break;
  }
  return n;
}
function ve(t) {
  const { top: o, right: e, bottom: n, left: r } = t;
  return [
    {
      x: r,
      y: o
    },
    {
      x: e,
      y: o
    },
    {
      x: e,
      y: n
    },
    {
      x: r,
      y: n
    }
  ];
}
function be(t, o) {
  const { x: e, y: n } = t;
  let r = !1;
  for (let i = 0, a = o.length - 1; i < o.length; a = i++) {
    const l = o[i].x, c = o[i].y, u = o[a].x, s = o[a].y;
    c > n != s > n && e < (u - l) * (n - c) / (s - c) + l && (r = !r);
  }
  return r;
}
function xe(t) {
  const o = t.slice();
  return o.sort((e, n) => e.x < n.x ? -1 : e.x > n.x ? 1 : e.y < n.y ? -1 : e.y > n.y ? 1 : 0), he(o);
}
function he(t) {
  if (t.length <= 1)
    return t.slice();
  const o = [];
  for (let n = 0; n < t.length; n++) {
    const r = t[n];
    for (; o.length >= 2; ) {
      const i = o[o.length - 1], a = o[o.length - 2];
      if ((i.x - a.x) * (r.y - a.y) >= (i.y - a.y) * (r.x - a.x))
        o.pop();
      else
        break;
    }
    o.push(r);
  }
  o.pop();
  const e = [];
  for (let n = t.length - 1; n >= 0; n--) {
    const r = t[n];
    for (; e.length >= 2; ) {
      const i = e[e.length - 1], a = e[e.length - 2];
      if ((i.x - a.x) * (r.y - a.y) >= (i.y - a.y) * (r.x - a.x))
        e.pop();
      else
        break;
    }
    e.push(r);
  }
  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);
}
const He = re, ke = ae, Se = se, Ne = ue;
export {
  Ne as Content,
  He as Provider,
  ke as Root,
  ae as Tooltip,
  ue as TooltipContent,
  re as TooltipProvider,
  se as TooltipTrigger,
  Se as Trigger,
  Ie as createTooltipScope
};
