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