import * as I from "react";
import { isElement as z } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useStableCallback as d } 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 { useIsoLayoutEffect as H } 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/useIsoLayoutEffect.js";
import { createChangeEventDetails as O } from "../../utils/createBaseUIEventDetails.js";
import { useFloatingTree as B, useFloatingParentNodeId as G } from "../components/FloatingTree.js";
import { useHoverInteractionSharedState as J, safePolygonIdentifier as x, isInteractiveElement as K } from "./useHoverInteractionSharedState.js";
import { triggerHover as A } from "../../utils/reason-parts.js";
import { getDocument as N, getTarget as Q } from "../utils/element.js";
import { isMouseLikePointerType as U } from "../utils/event.js";
const V = /* @__PURE__ */ new Set(["click", "mousedown"]);
function se(i, p = {}) {
  const r = "rootStore" in i ? i.rootStore : i, E = r.useState("open"), c = r.useState("floatingElement"), R = r.useState("domReferenceElement"), {
    dataRef: v
  } = r.context, {
    enabled: S = !0,
    closeDelay: M = 0,
    externalTree: W
  } = p, {
    pointerTypeRef: g,
    interactedInsideRef: m,
    handlerRef: a,
    performedPointerEventsMutationRef: y,
    unbindMouseMoveRef: j,
    restTimeoutPendingRef: T,
    openChangeTimeout: L,
    handleCloseOptionsRef: h
  } = J(r), b = B(W), C = G(), k = d(() => m.current ? !0 : v.current.openEvent ? V.has(v.current.openEvent.type) : !1), P = d(() => {
    var n;
    const e = (n = v.current.openEvent) == null ? void 0 : n.type;
    return (e == null ? void 0 : e.includes("mouse")) && e !== "mousedown";
  }), D = I.useCallback((e, n = !0) => {
    const s = X(M, g.current);
    s && !a.current ? L.start(s, () => r.setOpen(!1, O(A, e))) : n && (L.clear(), r.setOpen(!1, O(A, e)));
  }, [M, a, r, g, L]), l = d(() => {
    j.current(), a.current = void 0;
  }), f = d(() => {
    if (y.current) {
      const e = N(c).body;
      e.style.pointerEvents = "", e.removeAttribute(x), y.current = !1;
    }
  }), F = d((e) => {
    const n = Q(e);
    if (!K(n)) {
      m.current = !1;
      return;
    }
    m.current = !0;
  });
  H(() => {
    E || (g.current = void 0, T.current = !1, m.current = !1, l(), f());
  }, [E, g, T, m, l, f]), I.useEffect(() => () => {
    l();
  }, [l]), I.useEffect(() => f, [f]), H(() => {
    var e, n, s;
    if (S && E && (e = h.current) != null && e.blockPointerEvents && P() && z(R) && c) {
      y.current = !0;
      const t = N(c).body;
      t.setAttribute(x, "");
      const o = R, u = c, w = (s = (n = b == null ? void 0 : b.nodesRef.current.find((q) => q.id === C)) == null ? void 0 : n.context) == null ? void 0 : s.elements.floating;
      return w && (w.style.pointerEvents = ""), t.style.pointerEvents = "none", o.style.pointerEvents = "auto", u.style.pointerEvents = "auto", () => {
        t.style.pointerEvents = "", o.style.pointerEvents = "", u.style.pointerEvents = "";
      };
    }
  }, [S, E, R, c, h, P, b, C, y]), I.useEffect(() => {
    if (!S)
      return;
    function e(o) {
      if (k() || !v.current.floatingContext || !r.select("open"))
        return;
      const u = r.context.triggerElements;
      o.relatedTarget && u.hasElement(o.relatedTarget) || (f(), l(), k() || D(o));
    }
    function n(o) {
      var u;
      L.clear(), f(), (u = a.current) == null || u.call(a, o), l();
    }
    function s(o) {
      k() || D(o, !1);
    }
    const t = c;
    return t && (t.addEventListener("mouseleave", e), t.addEventListener("mouseenter", n), t.addEventListener("mouseleave", s), t.addEventListener("pointerdown", F, !0)), () => {
      t && (t.removeEventListener("mouseleave", e), t.removeEventListener("mouseenter", n), t.removeEventListener("mouseleave", s), t.removeEventListener("pointerdown", F, !0));
    };
  });
}
function X(i, p) {
  return p && !U(p) ? 0 : typeof i == "function" ? i() : i;
}
export {
  X as getDelay,
  se as useHoverFloatingInteraction
};
