import { useRef as a, useState as d } from "react";
import { getOwnerDocument as m } from "../utils/owner.js";
import { useDisposables as g } from "./use-disposables.js";
import { useEvent as u } from "./use-event.js";
function h(t) {
  let e = t.width / 2, n = t.height / 2;
  return { top: t.clientY - n, right: t.clientX + e, bottom: t.clientY + n, left: t.clientX - e };
}
function v(t, e) {
  return !(!t || !e || t.right < e.left || t.left > e.right || t.bottom < e.top || t.top > e.bottom);
}
function D({ disabled: t = !1 } = {}) {
  let e = a(null), [n, i] = d(!1), r = g(), o = u(() => {
    e.current = null, i(!1), r.dispose();
  }), p = u((l) => {
    if (r.dispose(), e.current === null) {
      e.current = l.currentTarget, i(!0);
      {
        let s = m(l.currentTarget);
        r.addEventListener(s, "pointerup", o, !1), r.addEventListener(s, "pointermove", (c) => {
          if (e.current) {
            let f = h(c);
            i(v(f, e.current.getBoundingClientRect()));
          }
        }, !1), r.addEventListener(s, "pointercancel", o, !1);
      }
    }
  });
  return { pressed: n, pressProps: t ? {} : { onPointerDown: p, onPointerUp: o, onClick: o } };
}
export {
  D as useActivePress
};
