/**
 * 触屏/笔：从左侧图元栏拖到画布的落点（HTML5 DragEvent + DataTransfer 在触摸上不可靠）
 */
import { useCallback, useEffect, useRef } from 'react';
import { sceneXYFromContentDropOffset } from "../utils/floorMapDropUtils";
var DRAG_THRESHOLD_PX = 8;
var POINTER_MOVE_OPTS = {
  capture: true,
  passive: false
};
export function useFloorMapPaletteTouchDrag(params) {
  var enabled = params.enabled,
    mapDropLayerRef = params.mapDropLayerRef,
    contentWidth = params.contentWidth,
    contentHeight = params.contentHeight,
    contentOffsetRef = params.contentOffsetRef,
    setMapPaletteDragActive = params.setMapPaletteDragActive,
    onDropInScene = params.onDropInScene;
  var metricsRef = useRef({
    cw: contentWidth,
    ch: contentHeight
  });
  metricsRef.current = {
    cw: contentWidth,
    ch: contentHeight
  };
  var sessionRef = useRef(null);
  var dragActivatedRef = useRef(false);
  var suppressNextClickRef = useRef(false);
  var removeListenersRef = useRef(null);
  var cleanupWindowListeners = useCallback(function () {
    var _removeListenersRef$c;
    (_removeListenersRef$c = removeListenersRef.current) === null || _removeListenersRef$c === void 0 || _removeListenersRef$c.call(removeListenersRef);
    removeListenersRef.current = null;
  }, []);
  useEffect(function () {
    return function () {
      cleanupWindowListeners();
      sessionRef.current = null;
      dragActivatedRef.current = false;
      setMapPaletteDragActive(false);
    };
  }, [cleanupWindowListeners, setMapPaletteDragActive]);
  var handlePaletteCardPointerDown = useCallback(function (payload, e) {
    if (!enabled) return;
    if (e.pointerType === 'mouse') return;
    if (e.button !== 0) return;
    cleanupWindowListeners();
    dragActivatedRef.current = false;
    sessionRef.current = {
      payload: payload,
      pointerId: e.pointerId,
      startX: e.clientX,
      startY: e.clientY
    };
    var onMove = function onMove(ev) {
      var s = sessionRef.current;
      if (!s || ev.pointerId !== s.pointerId) return;
      var dx = ev.clientX - s.startX;
      var dy = ev.clientY - s.startY;
      if (!dragActivatedRef.current && dx * dx + dy * dy >= Math.pow(DRAG_THRESHOLD_PX, 2)) {
        dragActivatedRef.current = true;
        setMapPaletteDragActive(true);
      }
      if (dragActivatedRef.current && ev.cancelable) {
        ev.preventDefault();
      }
    };
    var onUp = function onUp(ev) {
      var s = sessionRef.current;
      if (!s || ev.pointerId !== s.pointerId) return;
      cleanupWindowListeners();
      sessionRef.current = null;
      var wasDrag = dragActivatedRef.current;
      dragActivatedRef.current = false;
      setMapPaletteDragActive(false);
      if (!wasDrag) return;
      suppressNextClickRef.current = true;
      var layer = mapDropLayerRef.current;
      if (!layer) return;
      var r = layer.getBoundingClientRect();
      var _metricsRef$current = metricsRef.current,
        cw = _metricsRef$current.cw,
        ch = _metricsRef$current.ch;
      if (cw <= 0 || ch <= 0) return;
      if (ev.clientX < r.left || ev.clientX > r.right || ev.clientY < r.top || ev.clientY > r.bottom) {
        return;
      }
      var lx = (ev.clientX - r.left) / Math.max(r.width, 1) * cw;
      var ly = (ev.clientY - r.top) / Math.max(r.height, 1) * ch;
      var _sceneXYFromContentDr = sceneXYFromContentDropOffset(lx, ly, contentOffsetRef.current),
        x = _sceneXYFromContentDr.x,
        y = _sceneXYFromContentDr.y;
      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 = function () {
      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]);
  var consumeToolbarClickIfTouchDrag = useCallback(function () {
    if (!suppressNextClickRef.current) return false;
    suppressNextClickRef.current = false;
    return true;
  }, []);
  return {
    handlePaletteCardPointerDown: handlePaletteCardPointerDown,
    consumeToolbarClickIfTouchDrag: consumeToolbarClickIfTouchDrag
  };
}