import { o as Te } from "./rolldown-runtime.js";
import { i as Ie, r as Se } from "./vendor/react.js";
import { f as V } from "./safeReact.js";
import { u as ye } from "./useRenderElement.js";
import { n as H, t as Ee } from "./useIsoLayoutEffect.js";
import { t as ke } from "./useOnMount.js";
import { n as we, t as ae } from "./useTimeout.js";
import { n as F, r as ge } from "./shadowDom.js";
import { a as Re, r as Le } from "./element.js";
import { H as ie, _ as _e, k as ue, v as Ce } from "./popupStateMapping.js";
import { n as fe } from "./event.js";
import { t as me } from "./owner.js";
import { t as x } from "./addEventListener.js";
import { S as K, t as j } from "./createBaseUIEventDetails.js";
import { t as ne } from "./useValueAsRef.js";
var ee = /* @__PURE__ */ Te(Ie());
function De(n, c) {
  return c != null && !fe(c) ? 0 : typeof n == "function" ? n() : n;
}
function de(n, c, e) {
  const f = De(n, e);
  return typeof f == "number" ? f : f?.[c];
}
function he(n) {
  return typeof n == "function" ? n() : n;
}
function Oe(n, c) {
  return c || n === "click" || n === "mousedown";
}
var Fe = class Pe {
  constructor() {
    this.pointerType = void 0, this.interactedInside = !1, this.handler = void 0, this.blockMouseMove = !0, this.performedPointerEventsMutation = !1, this.pointerEventsScopeElement = null, this.pointerEventsReferenceElement = null, this.pointerEventsFloatingElement = null, this.restTimeoutPending = !1, this.openChangeTimeout = new ae(), this.restTimeout = new ae(), this.handleCloseOptions = void 0;
  }
  static create() {
    return new Pe();
  }
  dispose = () => {
    this.openChangeTimeout.clear(), this.restTimeout.clear();
  };
  disposeEffect = () => this.dispose;
}, le = /* @__PURE__ */ new WeakMap();
function ce(n) {
  if (!n.performedPointerEventsMutation) return;
  const c = n.pointerEventsScopeElement;
  c && le.get(c) === n && (n.pointerEventsScopeElement?.style.removeProperty("pointer-events"), n.pointerEventsReferenceElement?.style.removeProperty("pointer-events"), n.pointerEventsFloatingElement?.style.removeProperty("pointer-events"), le.delete(c)), n.performedPointerEventsMutation = !1, n.pointerEventsScopeElement = null, n.pointerEventsReferenceElement = null, n.pointerEventsFloatingElement = null;
}
function be(n, c) {
  const { scopeElement: e, referenceElement: f, floatingElement: s } = c, r = le.get(e);
  r && r !== n && ce(r), ce(n), n.performedPointerEventsMutation = !0, n.pointerEventsScopeElement = e, n.pointerEventsReferenceElement = f, n.pointerEventsFloatingElement = s, le.set(e, n), e.style.pointerEvents = "none", f.style.pointerEvents = "auto", s.style.pointerEvents = "auto";
}
function Me(n) {
  const c = ye(Fe.create).current, e = n.context.dataRef.current;
  return e.hoverInteractionState || (e.hoverInteractionState = c), ke(e.hoverInteractionState.disposeEffect), e.hoverInteractionState;
}
function et(n, c = {}) {
  const e = "rootStore" in n ? n.rootStore : n, f = e.useState("open"), s = e.useState("floatingElement"), r = e.useState("domReferenceElement"), { dataRef: L } = e.context, { enabled: y = !0, closeDelay: k = 0, nodeId: W } = c, l = Me(e), m = Ce(), b = _e(), _ = H(() => Oe(L.current.openEvent?.type, l.interactedInside)), X = H(() => {
    const p = L.current.openEvent?.type;
    return p?.includes("mouse") && p !== "mousedown";
  }), Z = H((p) => Re(p, e.context.triggerElements)), q = ee.useCallback((p) => {
    const v = de(k, "close", l.pointerType), E = () => {
      e.setOpen(!1, j(K, p)), m?.events.emit("floating.closed", p);
    };
    v ? l.openChangeTimeout.start(v, E) : (l.openChangeTimeout.clear(), E());
  }, [
    k,
    e,
    l,
    m
  ]), o = H(() => {
    ce(l);
  }), D = H((p) => {
    const v = ge(p);
    if (!Le(v)) {
      l.interactedInside = !1;
      return;
    }
    l.interactedInside = v?.closest("[aria-haspopup]") != null;
  });
  Ee(() => {
    f || (l.pointerType = void 0, l.restTimeoutPending = !1, l.interactedInside = !1, o());
  }, [
    f,
    l,
    o
  ]), ee.useEffect(() => o, [o]), Ee(() => {
    if (y && f && l.handleCloseOptions?.blockPointerEvents && X() && V(r) && s) {
      const p = r, v = s, E = me(s), g = m?.nodesRef.current.find((R) => R.id === b)?.context?.elements.floating;
      return g && (g.style.pointerEvents = ""), be(l, {
        scopeElement: l.handleCloseOptions?.getScope?.() ?? l.pointerEventsScopeElement ?? g ?? p.closest("[data-rootownerid]") ?? E.body,
        referenceElement: p,
        floatingElement: v
      }), () => {
        o();
      };
    }
  }, [
    y,
    f,
    r,
    s,
    l,
    X,
    m,
    b,
    o
  ]);
  const O = we();
  ee.useEffect(() => {
    if (!y) return;
    function p() {
      l.openChangeTimeout.clear(), O.clear(), m?.events.off("floating.closed", E), o();
    }
    function v(R) {
      if (m && b && ie(m.nodesRef.current, b).length > 0) {
        m.events.on("floating.closed", E);
        return;
      }
      if (Z(R.relatedTarget)) return;
      const w = L.current.floatingContext?.nodeId ?? W, U = R.relatedTarget;
      if (!(m && w && V(U) && ie(m.nodesRef.current, w, !1).some((te) => F(te.context?.elements.floating, U)))) {
        if (l.handler) {
          l.handler(R);
          return;
        }
        o(), _() || q(R);
      }
    }
    function E(R) {
      !m || !b || ie(m.nodesRef.current, b).length > 0 || O.start(0, () => {
        m.events.off("floating.closed", E), e.setOpen(!1, j(K, R)), m.events.emit("floating.closed", R);
      });
    }
    const g = s;
    return ue(g && x(g, "mouseenter", p), g && x(g, "mouseleave", v), g && x(g, "pointerdown", D, !0), () => {
      m?.events.off("floating.closed", E);
    });
  }, [
    y,
    s,
    e,
    L,
    W,
    _,
    Z,
    q,
    o,
    D,
    l,
    m,
    b,
    O
  ]);
}
var He = /* @__PURE__ */ Te(Se()), Xe = { current: null };
function tt(n, c = {}) {
  const e = "rootStore" in n ? n.rootStore : n, { dataRef: f, events: s } = e.context, { enabled: r = !0, delay: L = 0, handleClose: y = null, mouseOnly: k = !1, restMs: W = 0, move: l = !0, triggerElementRef: m = Xe, externalTree: b, isActiveTrigger: _ = !0, getHandleCloseContext: X, isClosing: Z } = c, q = Ce(b), o = Me(e), D = ee.useRef(!1), O = ne(y), p = ne(L), v = ne(W), E = ne(r), g = ne(Z);
  _ && (o.handleCloseOptions = O.current?.__options);
  const R = H(() => Oe(f.current.openEvent?.type, o.interactedInside)), w = H((i) => Re(i, e.context.triggerElements)), U = H((i, t, C) => {
    const a = e.context.triggerElements;
    if (a.hasElement(t)) return !i || !F(i, t);
    if (!V(C)) return !1;
    const T = C;
    return a.hasMatchingElement((h) => F(h, T)) && (!i || !F(i, T));
  }), te = H((i, t = !0) => {
    const C = de(p.current, "close", o.pointerType);
    C ? o.openChangeTimeout.start(C, () => {
      e.setOpen(!1, j(K, i)), q?.events.emit("floating.closed", i);
    }) : t && (o.openChangeTimeout.clear(), e.setOpen(!1, j(K, i)), q?.events.emit("floating.closed", i));
  }), Y = H(() => {
    o.handler && (me(e.select("domReferenceElement")).removeEventListener("mousemove", o.handler), o.handler = void 0);
  }), Q = H(() => {
    ce(o);
  });
  return ee.useEffect(() => Y, [Y]), ee.useEffect(() => {
    if (!r) return;
    function i(t) {
      t.open ? D.current = !1 : (D.current = t.reason === K, Y(), o.openChangeTimeout.clear(), o.restTimeout.clear(), o.blockMouseMove = !0, o.restTimeoutPending = !1);
    }
    return s.on("openchange", i), () => {
      s.off("openchange", i);
    };
  }, [
    r,
    s,
    o,
    Y
  ]), ee.useEffect(() => {
    if (!r) return;
    const i = m.current ?? (_ ? e.select("domReferenceElement") : null);
    if (!V(i)) return;
    function t(a) {
      if (o.openChangeTimeout.clear(), o.blockMouseMove = !1, k && !fe(o.pointerType)) return;
      const T = he(v.current), h = de(p.current, "open", o.pointerType), M = ge(a), B = a.currentTarget ?? null, I = e.select("domReferenceElement");
      let P = B;
      if (V(M) && !e.context.triggerElements.hasElement(M)) {
        for (const pe of e.context.triggerElements.elements()) if (F(pe, M)) {
          P = pe;
          break;
        }
      }
      V(B) && V(I) && !e.context.triggerElements.hasElement(B) && F(B, I) && (P = I);
      const $ = P == null ? !1 : U(I, P, M), G = e.select("open"), u = g.current?.() ?? e.select("transitionStatus") === "ending", N = !G && u && D.current, z = !$ && V(P) && V(I) && F(I, P) && N, S = T > 0 && !h, J = $ && (G || N) || z, A = !G || $;
      if (J) {
        e.setOpen(!0, j(K, a, P));
        return;
      }
      S || (h ? o.openChangeTimeout.start(h, () => {
        A && e.setOpen(!0, j(K, a, P));
      }) : A && e.setOpen(!0, j(K, a, P)));
    }
    function C(a) {
      if (R()) {
        Q();
        return;
      }
      Y();
      const T = me(e.select("domReferenceElement"));
      o.restTimeout.clear(), o.restTimeoutPending = !1;
      const h = f.current.floatingContext ?? X?.();
      if (!w(a.relatedTarget)) {
        if (O.current && h) {
          e.select("open") || o.openChangeTimeout.clear();
          const M = m.current;
          o.handler = O.current({
            ...h,
            tree: q,
            x: a.clientX,
            y: a.clientY,
            onClose() {
              Q(), Y(), E.current && !R() && M === e.select("domReferenceElement") && te(a, !0);
            }
          }), T.addEventListener("mousemove", o.handler), o.handler(a);
          return;
        }
        (o.pointerType !== "touch" || !F(e.select("floatingElement"), a.relatedTarget)) && te(a);
      }
    }
    return l ? ue(x(i, "mousemove", t, { once: !0 }), x(i, "mouseenter", t), x(i, "mouseleave", C)) : ue(x(i, "mouseenter", t), x(i, "mouseleave", C));
  }, [
    Y,
    Q,
    f,
    p,
    te,
    e,
    r,
    O,
    o,
    _,
    U,
    R,
    w,
    k,
    l,
    v,
    m,
    q,
    E,
    X,
    g
  ]), ee.useMemo(() => {
    if (!r) return;
    function i(t) {
      o.pointerType = t.pointerType;
    }
    return {
      onPointerDown: i,
      onPointerEnter: i,
      onMouseMove(t) {
        const { nativeEvent: C } = t, a = t.currentTarget, T = e.select("domReferenceElement"), h = e.select("open"), M = U(T, a, t.target);
        if (k && !fe(o.pointerType)) return;
        if (h && M && o.handleCloseOptions?.blockPointerEvents) {
          const P = e.select("floatingElement");
          P && be(o, {
            scopeElement: o.handleCloseOptions?.getScope?.() ?? a.ownerDocument.body,
            referenceElement: a,
            floatingElement: P
          });
        }
        const B = he(v.current);
        if (h && !M || B === 0 || !M && o.restTimeoutPending && t.movementX ** 2 + t.movementY ** 2 < 2) return;
        o.restTimeout.clear();
        function I() {
          if (o.restTimeoutPending = !1, R()) return;
          const P = e.select("open");
          !o.blockMouseMove && (!P || M) && e.setOpen(!0, j(K, C, a));
        }
        o.pointerType === "touch" ? He.flushSync(() => {
          I();
        }) : M && h ? I() : (o.restTimeoutPending = !0, o.restTimeout.start(B, I));
      }
    };
  }, [
    r,
    o,
    R,
    U,
    k,
    e,
    v
  ]);
}
var ve = 0.1, Ye = ve * ve, d = 0.5;
function oe(n, c, e, f, s, r) {
  return f >= c != r >= c && n <= (s - e) * (c - f) / (r - f) + e;
}
function re(n, c, e, f, s, r, L, y, k, W) {
  let l = !1;
  return oe(n, c, e, f, s, r) && (l = !l), oe(n, c, s, r, L, y) && (l = !l), oe(n, c, L, y, k, W) && (l = !l), oe(n, c, k, W, e, f) && (l = !l), l;
}
function Be(n, c, e) {
  return n >= e.x && n <= e.x + e.width && c >= e.y && c <= e.y + e.height;
}
function se(n, c, e, f, s, r) {
  return n >= Math.min(e, s) && n <= Math.max(e, s) && c >= Math.min(f, r) && c <= Math.max(f, r);
}
function nt(n = {}) {
  const { blockPointerEvents: c = !1 } = n, e = new ae(), f = ({ x: s, y: r, placement: L, elements: y, onClose: k, nodeId: W, tree: l }) => {
    const m = L?.split("-")[0];
    let b = !1, _ = null, X = null, Z = typeof performance < "u" ? performance.now() : 0;
    function q(D, O) {
      const p = performance.now(), v = p - Z;
      if (_ === null || X === null || v === 0)
        return _ = D, X = O, Z = p, !1;
      const E = D - _, g = O - X, R = E * E + g * g, w = v * v * Ye;
      return _ = D, X = O, Z = p, R < w;
    }
    function o() {
      e.clear(), k();
    }
    return function(O) {
      e.clear();
      const p = y.domReference, v = y.floating;
      if (!p || !v || m == null || s == null || r == null) return;
      const { clientX: E, clientY: g } = O, R = ge(O), w = O.type === "mouseleave", U = F(v, R), te = F(p, R);
      if (U && (b = !0, !w))
        return;
      if (te && (b = !1, !w)) {
        b = !0;
        return;
      }
      if (w && V(O.relatedTarget) && F(v, O.relatedTarget)) return;
      function Y() {
        return !!(l && ie(l.nodesRef.current, W).length > 0);
      }
      function Q() {
        Y() || o();
      }
      if (Y()) return;
      const i = p.getBoundingClientRect(), t = v.getBoundingClientRect(), C = s > t.right - t.width / 2, a = r > t.bottom - t.height / 2, T = t.width > i.width, h = t.height > i.height, M = (T ? i : t).left, B = (T ? i : t).right, I = (h ? i : t).top, P = (h ? i : t).bottom;
      if (m === "top" && r >= i.bottom - 1 || m === "bottom" && r <= i.top + 1 || m === "left" && s >= i.right - 1 || m === "right" && s <= i.left + 1) {
        Q();
        return;
      }
      let $ = !1;
      switch (m) {
        case "top":
          $ = se(E, g, M, i.top + 1, B, t.bottom - 1);
          break;
        case "bottom":
          $ = se(E, g, M, t.top + 1, B, i.bottom - 1);
          break;
        case "left":
          $ = se(E, g, t.right - 1, P, i.left + 1, I);
          break;
        case "right":
          $ = se(E, g, i.right - 1, P, t.left + 1, I);
          break;
        default:
      }
      if ($) return;
      if (b && !Be(E, g, i)) {
        Q();
        return;
      }
      if (!w && q(E, g)) {
        Q();
        return;
      }
      let G = !1;
      switch (m) {
        case "top": {
          const u = T ? d / 2 : d * 4, N = T || C ? s + u : s - u, z = T ? s - u : C ? s + u : s - u, S = r + d + 1, J = C || T ? t.bottom - d : t.top, A = C ? T ? t.bottom - d : t.top : t.bottom - d;
          G = re(E, g, N, S, z, S, t.left, J, t.right, A);
          break;
        }
        case "bottom": {
          const u = T ? d / 2 : d * 4, N = T || C ? s + u : s - u, z = T ? s - u : C ? s + u : s - u, S = r - d, J = C || T ? t.top + d : t.bottom, A = C ? T ? t.top + d : t.bottom : t.top + d;
          G = re(E, g, N, S, z, S, t.left, J, t.right, A);
          break;
        }
        case "left": {
          const u = h ? d / 2 : d * 4, N = h || a ? r + u : r - u, z = h ? r - u : a ? r + u : r - u, S = s + d + 1, J = a || h ? t.right - d : t.left, A = a ? h ? t.right - d : t.left : t.right - d;
          G = re(E, g, J, t.top, A, t.bottom, S, N, S, z);
          break;
        }
        case "right": {
          const u = h ? d / 2 : d * 4, N = h || a ? r + u : r - u, z = h ? r - u : a ? r + u : r - u, S = s - d, J = a || h ? t.left + d : t.right, A = a ? h ? t.left + d : t.right : t.left + d;
          G = re(E, g, S, N, S, z, J, t.top, A, t.bottom);
          break;
        }
        default:
      }
      G ? b || e.start(40, Q) : Q();
    };
  };
  return f.__options = {
    ...n,
    blockPointerEvents: c
  }, f;
}
export {
  de as i,
  tt as n,
  et as r,
  nt as t
};

//# sourceMappingURL=safePolygon.js.map