import * as H from "react";
import * as ne from "react-dom";
import { isElement as oe } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useValueAsRef as N } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useValueAsRef.js";
import { useStableCallback as Y } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { createChangeEventDetails as y } from "../../utils/createBaseUIEventDetails.js";
import { getDelay as V } from "./useHover.js";
import { useFloatingTree as ce } from "../components/FloatingTree.js";
import { useHoverInteractionSharedState as se, safePolygonIdentifier as ue } from "./useHoverInteractionSharedState.js";
import { triggerHover as O } from "../../utils/reason-parts.js";
import { getDocument as G, contains as x } from "../utils/element.js";
import { isMouseLikePointerType as J } from "../utils/event.js";
function W(f) {
  return typeof f == "function" ? f() : f;
}
const ie = {
  current: null
};
function ve(f, K = {}) {
  var z;
  const e = "rootStore" in f ? f.rootStore : f, {
    dataRef: m,
    events: A
  } = e.context, {
    enabled: D = !0,
    delay: Q = 0,
    handleClose: U = null,
    mouseOnly: b = !1,
    restMs: Z = 0,
    move: X = !0,
    triggerElementRef: P = ie,
    externalTree: $,
    isActiveTrigger: _ = !0
  } = K, w = ce($), {
    pointerTypeRef: o,
    interactedInsideRef: ee,
    handlerRef: v,
    blockMouseMoveRef: M,
    performedPointerEventsMutationRef: j,
    unbindMouseMoveRef: S,
    restTimeoutPendingRef: l,
    openChangeTimeout: c,
    restTimeout: g,
    handleCloseOptionsRef: te
  } = se(e), d = N(U), L = N(Q), C = N(Z);
  _ && (te.current = (z = d.current) == null ? void 0 : z.__options);
  const p = Y(() => ee.current ? !0 : m.current.openEvent ? ["click", "mousedown"].includes(m.current.openEvent.type) : !1), k = H.useCallback((t, n = !0) => {
    const s = V(L.current, "close", o.current);
    s && !v.current ? c.start(s, () => e.setOpen(!1, y(O, t))) : n && (c.clear(), e.setOpen(!1, y(O, t)));
  }, [L, v, e, o, c]), F = Y(() => {
    S.current(), v.current = void 0;
  }), I = Y(() => {
    if (j.current) {
      const t = G(e.select("domReferenceElement")).body;
      t.style.pointerEvents = "", t.removeAttribute(ue), j.current = !1;
    }
  });
  H.useEffect(() => {
    if (!D)
      return;
    function t(n) {
      n.open || (c.clear(), g.clear(), M.current = !0, l.current = !1);
    }
    return A.on("openchange", t), () => {
      A.off("openchange", t);
    };
  }, [D, A, c, g, M, l]);
  const q = Y((t) => {
    var E;
    if (p() || !m.current.floatingContext)
      return;
    const n = e.context.triggerElements;
    if (t.relatedTarget && n.hasElement(t.relatedTarget))
      return;
    const s = P.current;
    (E = d.current) == null || E.call(d, {
      ...m.current.floatingContext,
      tree: w,
      x: t.clientX,
      y: t.clientY,
      onClose() {
        I(), F(), !p() && s === e.select("domReferenceElement") && k(t);
      }
    })(t);
  });
  return H.useEffect(() => {
    if (!D)
      return;
    const t = P.current ?? (_ ? e.select("domReferenceElement") : null);
    if (!oe(t))
      return;
    function n(r) {
      if (c.clear(), M.current = !1, b && !J(o.current) || W(C.current) > 0 && !V(L.current, "open"))
        return;
      const R = V(L.current, "open", o.current), a = e.select("domReferenceElement"), u = e.context.triggerElements, h = (u.hasElement(r.target) || u.hasMatchingElement((re) => x(re, r.target))) && (!a || !x(a, r.target)), i = r.currentTarget ?? null, T = e.select("open"), B = !T || h;
      h && T ? e.setOpen(!0, y(O, r, i)) : R ? c.start(R, () => {
        B && e.setOpen(!0, y(O, r, i));
      }) : B && e.setOpen(!0, y(O, r, i));
    }
    function s(r) {
      if (p()) {
        I();
        return;
      }
      S.current();
      const R = e.select("domReferenceElement"), a = G(R);
      g.clear(), l.current = !1;
      const u = e.context.triggerElements;
      if (r.relatedTarget && u.hasElement(r.relatedTarget))
        return;
      if (d.current && m.current.floatingContext) {
        e.select("open") || c.clear();
        const i = P.current;
        v.current = d.current({
          ...m.current.floatingContext,
          tree: w,
          x: r.clientX,
          y: r.clientY,
          onClose() {
            I(), F(), !p() && i === e.select("domReferenceElement") && k(r, !0);
          }
        });
        const T = v.current;
        T(r), a.addEventListener("mousemove", T), S.current = () => {
          a.removeEventListener("mousemove", T);
        };
        return;
      }
      (o.current === "touch" ? !x(e.select("floatingElement"), r.relatedTarget) : !0) && k(r);
    }
    function E(r) {
      q(r);
    }
    return e.select("open") && t.addEventListener("mouseleave", E), X && t.addEventListener("mousemove", n, {
      once: !0
    }), t.addEventListener("mouseenter", n), t.addEventListener("mouseleave", s), () => {
      t.removeEventListener("mouseleave", E), X && t.removeEventListener("mousemove", n), t.removeEventListener("mouseenter", n), t.removeEventListener("mouseleave", s);
    };
  }, [F, I, M, m, L, k, e, D, d, q, _, p, b, X, o, C, g, l, c, P, w, S, v]), H.useMemo(() => {
    function t(n) {
      o.current = n.pointerType;
    }
    return {
      onPointerDown: t,
      onPointerEnter: t,
      onMouseMove(n) {
        const {
          nativeEvent: s
        } = n, E = n.currentTarget, r = e.select("domReferenceElement"), R = e.context.triggerElements, a = e.select("open"), u = (R.hasElement(n.target) || R.hasMatchingElement((i) => x(i, n.target))) && (!r || !x(r, n.target));
        if (b && !J(o.current) || a && !u || W(C.current) === 0 || !u && l.current && n.movementX ** 2 + n.movementY ** 2 < 2)
          return;
        g.clear();
        function h() {
          if (l.current = !1, p())
            return;
          const i = e.select("open");
          !M.current && (!i || u) && e.setOpen(!0, y(O, s, E));
        }
        o.current === "touch" ? ne.flushSync(() => {
          h();
        }) : u && a ? h() : (l.current = !0, g.start(W(C.current), h));
      }
    };
  }, [M, p, b, e, o, C, g, l]);
}
export {
  ve as useHoverReferenceInteraction
};
