import { useState as x, useRef as l, useEffect as g, useCallback as D } from "react";
function X(o) {
  const [n, c] = x({ x: 0, y: 0 }), r = l({ x: 0, y: 0 }), t = l({ startX: 0, startY: 0, isDragging: !1 });
  g(() => {
    r.current = n;
  }, [n]), g(() => {
    o || c({ x: 0, y: 0 });
  }, [o]);
  const d = D((e) => {
    const s = e.target;
    if (s.closest("button") || s.closest("input") || s.closest("select") || s.closest("a"))
      return;
    e.preventDefault(), t.current = {
      isDragging: !0,
      startX: e.clientX,
      startY: e.clientY
    };
    const m = r.current.x, f = r.current.y, u = (i) => {
      if (!t.current.isDragging) return;
      const v = i.clientX - t.current.startX, p = i.clientY - t.current.startY;
      c({
        x: m + v,
        y: f + p
      });
    }, a = () => {
      t.current.isDragging = !1, document.removeEventListener("mousemove", u), document.removeEventListener("mouseup", a);
    };
    document.addEventListener("mousemove", u), document.addEventListener("mouseup", a);
  }, []);
  return { position: n, handleMouseDown: d };
}
export {
  X as u
};
