import { useEffect, useState } from "react";
const useDraggableModal = ({
  dragHandleSelector,
  modalElement
}) => {
  const [coords, setCoords] = useState(null);
  useEffect(() => {
    const dragTarget = modalElement?.querySelector(dragHandleSelector);
    if (!dragTarget) {
      return () => {
      };
    }
    let clickOffset = { x: 0, y: 0 };
    let isDragging = false;
    const onDragStart = ({ clientX, clientY, button }) => {
      if (button !== 0) {
        return;
      }
      const modalRect = modalElement?.getBoundingClientRect();
      clickOffset = {
        x: clientX - (modalRect?.x || 0),
        y: clientY - (modalRect?.y || 0)
      };
      isDragging = true;
    };
    const onDrag = ({ clientX, clientY }) => {
      if (!isDragging) {
        return;
      }
      setCoords({
        x: clientX - clickOffset.x,
        y: Math.max(0, clientY - clickOffset.y)
      });
    };
    const onDragEnd = () => {
      isDragging = false;
    };
    if (dragTarget) {
      dragTarget.addEventListener("mousedown", onDragStart);
      window.addEventListener("mousemove", onDrag);
      window.addEventListener("mouseup", onDragEnd);
    }
    return () => {
      if (dragTarget) {
        dragTarget.removeEventListener("mousedown", onDragStart);
        window.removeEventListener("mousemove", onDrag);
        window.removeEventListener("mouseup", onDragEnd);
      }
    };
  }, [modalElement]);
  return coords;
};
export {
  useDraggableModal
};
//# sourceMappingURL=use-draggable-modal.js.map
