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/components/pisellFloorMapLayout/hooks/useFloorMapPaletteTouchDrag.ts
var useFloorMapPaletteTouchDrag_exports = {};
__export(useFloorMapPaletteTouchDrag_exports, {
  useFloorMapPaletteTouchDrag: () => useFloorMapPaletteTouchDrag
});
module.exports = __toCommonJS(useFloorMapPaletteTouchDrag_exports);
var import_react = require("react");
var import_floorMapDropUtils = require("../utils/floorMapDropUtils");
var DRAG_THRESHOLD_PX = 8;
var POINTER_MOVE_OPTS = {
  capture: true,
  passive: false
};
function useFloorMapPaletteTouchDrag(params) {
  const {
    enabled,
    mapDropLayerRef,
    contentWidth,
    contentHeight,
    contentOffsetRef,
    setMapPaletteDragActive,
    onDropInScene
  } = params;
  const metricsRef = (0, import_react.useRef)({ cw: contentWidth, ch: contentHeight });
  metricsRef.current = { cw: contentWidth, ch: contentHeight };
  const sessionRef = (0, import_react.useRef)(null);
  const dragActivatedRef = (0, import_react.useRef)(false);
  const suppressNextClickRef = (0, import_react.useRef)(false);
  const removeListenersRef = (0, import_react.useRef)(null);
  const cleanupWindowListeners = (0, import_react.useCallback)(() => {
    var _a;
    (_a = removeListenersRef.current) == null ? void 0 : _a.call(removeListenersRef);
    removeListenersRef.current = null;
  }, []);
  (0, import_react.useEffect)(
    () => () => {
      cleanupWindowListeners();
      sessionRef.current = null;
      dragActivatedRef.current = false;
      setMapPaletteDragActive(false);
    },
    [cleanupWindowListeners, setMapPaletteDragActive]
  );
  const handlePaletteCardPointerDown = (0, import_react.useCallback)(
    (payload, e) => {
      if (!enabled) return;
      if (e.pointerType === "mouse") return;
      if (e.button !== 0) return;
      cleanupWindowListeners();
      dragActivatedRef.current = false;
      sessionRef.current = {
        payload,
        pointerId: e.pointerId,
        startX: e.clientX,
        startY: e.clientY
      };
      const onMove = (ev) => {
        const s = sessionRef.current;
        if (!s || ev.pointerId !== s.pointerId) return;
        const dx = ev.clientX - s.startX;
        const dy = ev.clientY - s.startY;
        if (!dragActivatedRef.current && dx * dx + dy * dy >= DRAG_THRESHOLD_PX ** 2) {
          dragActivatedRef.current = true;
          setMapPaletteDragActive(true);
        }
        if (dragActivatedRef.current && ev.cancelable) {
          ev.preventDefault();
        }
      };
      const onUp = (ev) => {
        const s = sessionRef.current;
        if (!s || ev.pointerId !== s.pointerId) return;
        cleanupWindowListeners();
        sessionRef.current = null;
        const wasDrag = dragActivatedRef.current;
        dragActivatedRef.current = false;
        setMapPaletteDragActive(false);
        if (!wasDrag) return;
        suppressNextClickRef.current = true;
        const layer = mapDropLayerRef.current;
        if (!layer) return;
        const r = layer.getBoundingClientRect();
        const { cw, ch } = metricsRef.current;
        if (cw <= 0 || ch <= 0) return;
        if (ev.clientX < r.left || ev.clientX > r.right || ev.clientY < r.top || ev.clientY > r.bottom) {
          return;
        }
        const lx = (ev.clientX - r.left) / Math.max(r.width, 1) * cw;
        const ly = (ev.clientY - r.top) / Math.max(r.height, 1) * ch;
        const { x, y } = (0, import_floorMapDropUtils.sceneXYFromContentDropOffset)(
          lx,
          ly,
          contentOffsetRef.current
        );
        onDropInScene(s.payload, x, y);
      };
      window.addEventListener("pointermove", onMove, POINTER_MOVE_OPTS);
      window.addEventListener("pointerup", onUp, { capture: true });
      window.addEventListener("pointercancel", onUp, { capture: true });
      removeListenersRef.current = () => {
        window.removeEventListener("pointermove", onMove, POINTER_MOVE_OPTS);
        window.removeEventListener("pointerup", onUp, { capture: true });
        window.removeEventListener("pointercancel", onUp, { capture: true });
      };
    },
    [
      enabled,
      mapDropLayerRef,
      contentOffsetRef,
      setMapPaletteDragActive,
      onDropInScene,
      cleanupWindowListeners
    ]
  );
  const consumeToolbarClickIfTouchDrag = (0, import_react.useCallback)(() => {
    if (!suppressNextClickRef.current) return false;
    suppressNextClickRef.current = false;
    return true;
  }, []);
  return {
    handlePaletteCardPointerDown,
    consumeToolbarClickIfTouchDrag
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFloorMapPaletteTouchDrag
});
