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);

// src/dragger/index.tsx
var dragger_exports = {};
__export(dragger_exports, {
  default: () => dragger_default
});
module.exports = __toCommonJS(dragger_exports);
var import_react = require("react");
function Dragger({ children, disabled, offset = 5, limit = { top: 0 } }) {
  var _a;
  const floatBtnRef = (0, import_react.useRef)(null);
  const isMouseDownRef = (0, import_react.useRef)(false);
  const mouseStartPosition = (0, import_react.useRef)({ x: 0, y: 0 });
  const mouseMovePosition = (0, import_react.useRef)({ x: 0, y: 0 });
  (0, import_react.useLayoutEffect)(() => {
    const moveCallback = (event) => {
      const { clientX, clientY } = event;
      if (!isMouseDownRef.current || !floatBtnRef.current)
        return;
      if (limit) {
        const element = floatBtnRef.current;
        const elementWidth = element.offsetWidth;
        const elementHeight = element.offsetHeight;
        const viewportWidth = window.innerWidth;
        const viewportHeight = window.innerHeight;
        let newLeft = clientX - mouseMovePosition.current.x;
        let newTop = clientY - mouseMovePosition.current.y;
        if (limit.left !== void 0) {
          newLeft = Math.max(limit.left, newLeft);
        }
        if (limit.right !== void 0) {
          newLeft = Math.min(viewportWidth - elementWidth - limit.right, newLeft);
        }
        if (limit.top !== void 0) {
          newTop = Math.max(limit.top, newTop);
        }
        if (limit.bottom !== void 0) {
          newTop = Math.min(viewportHeight - elementHeight - limit.bottom, newTop);
        }
        element.style.left = `${newLeft}px`;
        element.style.top = `${newTop}px`;
        element.style.bottom = "unset";
        element.style.right = "unset";
      } else {
        floatBtnRef.current.style.left = `${clientX - mouseMovePosition.current.x}px`;
        floatBtnRef.current.style.top = `${clientY - mouseMovePosition.current.y}px`;
        floatBtnRef.current.style.bottom = "unset";
        floatBtnRef.current.style.right = "unset";
      }
    };
    document.addEventListener("mousemove", moveCallback);
    return () => {
      document.removeEventListener("mousemove", moveCallback);
    };
  }, [limit]);
  const handleMouseStart = (event) => {
    if (disabled)
      return;
    isMouseDownRef.current = true;
    const { clientX, clientY } = event;
    const { offsetLeft, offsetTop } = floatBtnRef.current;
    mouseStartPosition.current = { x: clientX, y: clientY };
    mouseMovePosition.current = { x: clientX - offsetLeft, y: clientY - offsetTop };
  };
  const handleMouseStop = () => {
    isMouseDownRef.current = false;
  };
  const handleDragStart = (event) => {
    event.preventDefault();
    event.stopPropagation();
  };
  const onDraggerClick = (event) => {
    var _a2, _b;
    const moveDistance = Math.sqrt(
      Math.pow(event.clientX - mouseStartPosition.current.x, 2) + Math.pow(event.clientY - mouseStartPosition.current.y, 2)
    );
    if (moveDistance < offset) {
      (_b = (_a2 = children.props).onClick) == null ? void 0 : _b.call(_a2, event);
    }
  };
  return (0, import_react.cloneElement)(children, {
    ...children.props,
    ref: floatBtnRef,
    style: { ...(_a = children.props) == null ? void 0 : _a.style, position: "fixed" },
    onClick: onDraggerClick,
    onMouseDown: handleMouseStart,
    onMouseUp: handleMouseStop,
    onDragStart: handleDragStart
  });
}
var dragger_default = Dragger;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
