require("../../../_virtual/_rolldown/runtime.js");
const require_floorMapDropUtils = require("../utils/floorMapDropUtils.js");
let react = require("react");
//#region src/components/pisellFloorMapLayout/hooks/useFloorMapPaletteTouchDrag.ts
/**
* 触屏/笔：从左侧图元栏拖到画布的落点（HTML5 DragEvent + DataTransfer 在触摸上不可靠）
*/
const DRAG_THRESHOLD_PX = 8;
const POINTER_MOVE_OPTS = {
	capture: true,
	passive: false
};
function useFloorMapPaletteTouchDrag(params) {
	const { enabled, mapDropLayerRef, contentWidth, contentHeight, contentOffsetRef, setMapPaletteDragActive, onDropInScene } = params;
	const metricsRef = (0, react.useRef)({
		cw: contentWidth,
		ch: contentHeight
	});
	metricsRef.current = {
		cw: contentWidth,
		ch: contentHeight
	};
	const sessionRef = (0, react.useRef)(null);
	const dragActivatedRef = (0, react.useRef)(false);
	const suppressNextClickRef = (0, react.useRef)(false);
	const removeListenersRef = (0, react.useRef)(null);
	const cleanupWindowListeners = (0, react.useCallback)(() => {
		var _removeListenersRef$c;
		(_removeListenersRef$c = removeListenersRef.current) === null || _removeListenersRef$c === void 0 || _removeListenersRef$c.call(removeListenersRef);
		removeListenersRef.current = null;
	}, []);
	(0, react.useEffect)(() => () => {
		cleanupWindowListeners();
		sessionRef.current = null;
		dragActivatedRef.current = false;
		setMapPaletteDragActive(false);
	}, [cleanupWindowListeners, setMapPaletteDragActive]);
	return {
		handlePaletteCardPointerDown: (0, 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 >= Math.pow(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 { x, y } = require_floorMapDropUtils.sceneXYFromContentDropOffset((ev.clientX - r.left) / Math.max(r.width, 1) * cw, (ev.clientY - r.top) / Math.max(r.height, 1) * ch, 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
		]),
		consumeToolbarClickIfTouchDrag: (0, react.useCallback)(() => {
			if (!suppressNextClickRef.current) return false;
			suppressNextClickRef.current = false;
			return true;
		}, [])
	};
}
//#endregion
exports.useFloorMapPaletteTouchDrag = useFloorMapPaletteTouchDrag;
