import { nodeContains as u } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/DOMFunctions.js";
import { getOwnerDocument as P } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/domHelpers.js";
import { useGlobalListeners as M } from "../../../../../@react-aria_utils@3.33.0_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@react-aria/utils/dist/useGlobalListeners.js";
import { useState as G, useRef as D, useEffect as H, useMemo as N } from "react";
let f = !1, d = 0;
function E() {
  f = !0, setTimeout(() => {
    f = !1;
  }, 50);
}
function h(l) {
  l.pointerType === "touch" && E();
}
function x() {
  if (!(typeof document > "u"))
    return d === 0 && (typeof PointerEvent < "u" ? document.addEventListener("pointerup", h) : process.env.NODE_ENV === "test" && document.addEventListener("touchend", E)), d++, () => {
      d--, !(d > 0) && (typeof PointerEvent < "u" ? document.removeEventListener("pointerup", h) : process.env.NODE_ENV === "test" && document.removeEventListener("touchend", E));
    };
}
function _(l) {
  let { onHoverStart: v, onHoverChange: s, onHoverEnd: p, isDisabled: r } = l, [T, c] = G(!1), t = D({
    isHovered: !1,
    ignoreEmulatedMouseEvents: !1,
    pointerType: "",
    target: null
  }).current;
  H(x, []);
  let { addGlobalListener: m, removeAllGlobalListeners: $ } = M(), { hoverProps: y, triggerHoverEnd: L } = N(() => {
    let b = (e, n) => {
      if (t.pointerType = n, r || n === "touch" || t.isHovered || !u(e.currentTarget, e.target)) return;
      t.isHovered = !0;
      let i = e.currentTarget;
      t.target = i, m(P(e.target), "pointerover", (g) => {
        t.isHovered && t.target && !u(t.target, g.target) && a(g, g.pointerType);
      }, {
        capture: !0
      }), v && v({
        type: "hoverstart",
        target: i,
        pointerType: n
      }), s && s(!0), c(!0);
    }, a = (e, n) => {
      let i = t.target;
      t.pointerType = "", t.target = null, !(n === "touch" || !t.isHovered || !i) && (t.isHovered = !1, $(), p && p({
        type: "hoverend",
        target: i,
        pointerType: n
      }), s && s(!1), c(!1));
    }, o = {};
    return typeof PointerEvent < "u" ? (o.onPointerEnter = (e) => {
      f && e.pointerType === "mouse" || b(e, e.pointerType);
    }, o.onPointerLeave = (e) => {
      !r && u(e.currentTarget, e.target) && a(e, e.pointerType);
    }) : process.env.NODE_ENV === "test" && (o.onTouchStart = () => {
      t.ignoreEmulatedMouseEvents = !0;
    }, o.onMouseEnter = (e) => {
      !t.ignoreEmulatedMouseEvents && !f && b(e, "mouse"), t.ignoreEmulatedMouseEvents = !1;
    }, o.onMouseLeave = (e) => {
      !r && u(e.currentTarget, e.target) && a(e, "mouse");
    }), {
      hoverProps: o,
      triggerHoverEnd: a
    };
  }, [
    v,
    s,
    p,
    r,
    t,
    m,
    $
  ]);
  return H(() => {
    r && L({
      currentTarget: t.target
    }, t.pointerType);
  }, [
    r
  ]), {
    hoverProps: y,
    isHovered: T
  };
}
export {
  _ as useHover
};
