"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_draggable_modal_exports = {};
__export(use_draggable_modal_exports, {
  useDraggableModal: () => useDraggableModal
});
module.exports = __toCommonJS(use_draggable_modal_exports);
var import_react = require("react");
const useDraggableModal = ({
  dragHandleSelector,
  modalElement
}) => {
  const [coords, setCoords] = (0, import_react.useState)(null);
  (0, import_react.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;
};
//# sourceMappingURL=use-draggable-modal.js.map
