import * as p from "react";
import { getWindow as P } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { useStableCallback as T } 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 k } 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 { isMouseLikePointerType as X } from "../utils/event.js";
import { getTarget as A, contains as Y } from "../utils/element.js";
function B(s, e) {
  let u = null, h = null, f = !1;
  return {
    contextElement: s || void 0,
    getBoundingClientRect() {
      var y;
      const t = (s == null ? void 0 : s.getBoundingClientRect()) || {
        width: 0,
        height: 0,
        x: 0,
        y: 0
      }, x = e.axis === "x" || e.axis === "both", n = e.axis === "y" || e.axis === "both", R = ["mouseenter", "mousemove"].includes(((y = e.dataRef.current.openEvent) == null ? void 0 : y.type) || "") && e.pointerType !== "touch";
      let r = t.width, o = t.height, l = t.x, c = t.y;
      return u == null && e.x && x && (u = t.x - e.x), h == null && e.y && n && (h = t.y - e.y), l -= u || 0, c -= h || 0, r = 0, o = 0, !f || R ? (r = e.axis === "y" ? t.width : 0, o = e.axis === "x" ? t.height : 0, l = x && e.x != null ? e.x : l, c = n && e.y != null ? e.y : c) : f && !R && (o = e.axis === "x" ? t.height : o, r = e.axis === "y" ? t.width : r), f = !0, {
        width: r,
        height: o,
        x: l,
        y: c,
        top: c,
        right: l + r,
        bottom: c + o,
        left: l
      };
    }
  };
}
function b(s) {
  return s != null && s.clientX != null;
}
function W(s, e = {}) {
  const u = "rootStore" in s ? s.rootStore : s, h = u.useState("open"), f = u.useState("floatingElement"), t = u.useState("domReferenceElement"), x = u.context.dataRef, {
    enabled: n = !0,
    axis: R = "both",
    x: r = null,
    y: o = null
  } = e, l = p.useRef(!1), c = p.useRef(null), [y, w] = p.useState(), [S, C] = p.useState([]), g = T((i, a, m) => {
    l.current || x.current.openEvent && !b(x.current.openEvent) || u.set("positionReference", B(m ?? t, {
      x: i,
      y: a,
      axis: R,
      dataRef: x,
      pointerType: y
    }));
  }), v = T((i) => {
    r != null || o != null || (h ? c.current || C([]) : g(i.clientX, i.clientY, i.currentTarget));
  }), M = X(y) ? f : h, d = p.useCallback(() => {
    if (!M || !n || r != null || o != null)
      return;
    const i = P(f);
    function a(m) {
      const L = A(m);
      Y(f, L) ? (i.removeEventListener("mousemove", a), c.current = null) : g(m.clientX, m.clientY);
    }
    if (!x.current.openEvent || b(x.current.openEvent)) {
      i.addEventListener("mousemove", a);
      const m = () => {
        i.removeEventListener("mousemove", a), c.current = null;
      };
      return c.current = m, m;
    }
    u.set("positionReference", t);
  }, [M, n, r, o, f, x, t, u, g]);
  p.useEffect(() => d(), [d, S]), p.useEffect(() => {
    n && !f && (l.current = !1);
  }, [n, f]), p.useEffect(() => {
    !n && h && (l.current = !0);
  }, [n, h]), k(() => {
    n && (r != null || o != null) && (l.current = !1, g(r, o));
  }, [n, r, o, g]);
  const E = p.useMemo(() => {
    function i(a) {
      w(a.pointerType);
    }
    return {
      onPointerDown: i,
      onPointerEnter: i,
      onMouseMove: v,
      onMouseEnter: v
    };
  }, [v]);
  return p.useMemo(() => n ? {
    reference: E,
    trigger: E
  } : {}, [n, E]);
}
export {
  W as useClientPoint
};
