/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_runtime = require("../../../_virtual/_rolldown/runtime.cjs");
const require_components_client_BottomSheet_utils = require("./utils.cjs");
let react = require("react");
let lodash_clamp_js = require("lodash/clamp.js");
lodash_clamp_js = require_runtime.__toESM(lodash_clamp_js, 1);
//#region src/components/client/BottomSheet/useBottomSheetDrag.ts
const DEFAULT_SCROLL_LOCK_TIMEOUT_MS = 100;
const OPEN_SCROLL_LOCK_TIMEOUT_MS = 500;
/**
* Drag-to-dismiss velocity threshold in pixels/second. A fast downward swipe
* exceeding this speed dismisses the sheet (regardless of distance dragged).
*/
const DRAG_DISMISS_VELOCITY_PX_PER_SECOND = 400;
function useBottomSheetDrag({ enableDrag, dismissible, store, sheetRef, isDraggingRef, activeSnapIndex, snapPointTranslatesPx, maxSnapPointPx, resolvedSnapPointsLength, resolvedDismissThresholdPx, emitSnapPointChange, setTranslateImmediate, openTimeRef, lastTimeDragPreventedRef }) {
	const isAllowedToDragRef = (0, react.useRef)(false);
	const dragStartTimeRef = (0, react.useRef)(null);
	const pointerStartRef = (0, react.useRef)(null);
	const dragStartTranslateRef = (0, react.useRef)(0);
	const dragStartIndexRef = (0, react.useRef)(0);
	const shouldDragFromTarget = (0, react.useCallback)((target, isDraggingUp) => {
		if (!sheetRef.current) return false;
		if (require_components_client_BottomSheet_utils.shouldPreventDragFromTarget(target)) return false;
		const selection = window.getSelection();
		if (selection?.toString() && sheetRef.current?.contains(selection.anchorNode)) return false;
		const now = Date.now();
		const openTime = openTimeRef.current;
		if (openTime && now - openTime < OPEN_SCROLL_LOCK_TIMEOUT_MS) return false;
		const currentHeight = sheetRef.current.getBoundingClientRect().height;
		if (currentHeight < maxSnapPointPx) return true;
		const lastPrevented = lastTimeDragPreventedRef.current;
		if (lastPrevented && now - lastPrevented < DEFAULT_SCROLL_LOCK_TIMEOUT_MS && currentHeight >= maxSnapPointPx) return false;
		if (isDraggingUp) {
			lastTimeDragPreventedRef.current = now;
			return false;
		}
		let element = target instanceof HTMLElement ? target : null;
		while (element) {
			if (require_components_client_BottomSheet_utils.isScrollable(element)) {
				if (element.scrollTop !== 0) {
					lastTimeDragPreventedRef.current = now;
					return false;
				}
			}
			if (element === sheetRef.current || element.getAttribute("role") === "dialog") return true;
			element = element.parentNode;
		}
		return true;
	}, [
		lastTimeDragPreventedRef,
		maxSnapPointPx,
		openTimeRef,
		sheetRef
	]);
	const cancelDrag = (0, react.useCallback)(() => {
		isDraggingRef.current = false;
		isAllowedToDragRef.current = false;
		dragStartTimeRef.current = null;
		pointerStartRef.current = null;
		sheetRef.current?.style.removeProperty("transition");
	}, [isDraggingRef, sheetRef]);
	const onPointerDown = (0, react.useCallback)((event) => {
		if (!enableDrag) return;
		if (event.button !== 0) return;
		if (require_components_client_BottomSheet_utils.shouldPreventDragFromTarget(event.target)) return;
		isDraggingRef.current = false;
		isAllowedToDragRef.current = false;
		dragStartTimeRef.current = performance.now();
		pointerStartRef.current = {
			x: event.pageX,
			y: event.pageY
		};
		dragStartTranslateRef.current = maxSnapPointPx - (sheetRef.current?.getBoundingClientRect().height ?? 0);
		dragStartIndexRef.current = activeSnapIndex;
	}, [
		activeSnapIndex,
		enableDrag,
		isDraggingRef,
		maxSnapPointPx,
		sheetRef
	]);
	const onPointerMove = (0, react.useCallback)((event) => {
		if (!enableDrag) return;
		const startPoint = pointerStartRef.current;
		const sheetEl = sheetRef.current;
		if (!startPoint || !sheetEl) return;
		if (!isAllowedToDragRef.current && event.buttons === 0) {
			cancelDrag();
			return;
		}
		const { x: startX, y: startY } = startPoint;
		const dx = event.pageX - startX;
		const dy = event.pageY - startY;
		const swipeStartThreshold = event.pointerType === "touch" ? 10 : 2;
		if (!isAllowedToDragRef.current) {
			if (Math.abs(dy) < swipeStartThreshold) return;
			if (Math.abs(dx) > Math.abs(dy)) {
				pointerStartRef.current = null;
				return;
			}
		}
		const isDraggingUp = startY - event.pageY > 0;
		if (!isAllowedToDragRef.current && !shouldDragFromTarget(event.target, isDraggingUp)) return;
		if (!isAllowedToDragRef.current) {
			event.currentTarget.setPointerCapture(event.pointerId);
			isAllowedToDragRef.current = true;
			isDraggingRef.current = true;
		}
		sheetEl.style.transition = "none";
		setTranslateImmediate((0, lodash_clamp_js.default)(dragStartTranslateRef.current + dy, 0, maxSnapPointPx));
	}, [
		enableDrag,
		isDraggingRef,
		maxSnapPointPx,
		setTranslateImmediate,
		shouldDragFromTarget,
		sheetRef
	]);
	const onPointerUp = (0, react.useCallback)((event) => {
		if (!enableDrag) return;
		if (!pointerStartRef.current || !sheetRef.current) {
			cancelDrag();
			return;
		}
		if (!isAllowedToDragRef.current) {
			cancelDrag();
			return;
		}
		const { y: startY } = pointerStartRef.current;
		const draggedDistance = startY - event.pageY;
		const timeTakenMs = Math.max(performance.now() - (dragStartTimeRef.current ?? performance.now()), 1);
		const velocityPxPerSecond = Math.abs(draggedDistance) / timeTakenMs * 1e3;
		const currentTranslatePx = maxSnapPointPx - sheetRef.current.getBoundingClientRect().height;
		const draggedDownBeyondLowestPx = currentTranslatePx - (snapPointTranslatesPx[dragStartIndexRef.current] ?? maxSnapPointPx);
		const draggedDown = draggedDistance < 0;
		const draggedUp = draggedDistance > 0;
		const isAtLowestSnapPoint = dragStartIndexRef.current === 0;
		if (dismissible && isAtLowestSnapPoint && draggedDown && (velocityPxPerSecond >= DRAG_DISMISS_VELOCITY_PX_PER_SECOND || draggedDownBeyondLowestPx >= resolvedDismissThresholdPx)) {
			cancelDrag();
			store.hide();
			return;
		}
		const lastIndex = resolvedSnapPointsLength - 1;
		const startIndex = dragStartIndexRef.current;
		if (velocityPxPerSecond >= DRAG_DISMISS_VELOCITY_PX_PER_SECOND && (draggedUp || draggedDown)) {
			const nextIndex = require_components_client_BottomSheet_utils.clampIndex(startIndex + (draggedUp ? 1 : -1), resolvedSnapPointsLength);
			cancelDrag();
			emitSnapPointChange(nextIndex);
			return;
		}
		let closestIndex = 0;
		let closestDist = Number.POSITIVE_INFINITY;
		for (let i = 0; i <= lastIndex; i++) {
			const dist = Math.abs((snapPointTranslatesPx[i] ?? 0) - currentTranslatePx);
			if (dist < closestDist) {
				closestDist = dist;
				closestIndex = i;
			}
		}
		cancelDrag();
		emitSnapPointChange(closestIndex);
	}, [
		cancelDrag,
		dismissible,
		emitSnapPointChange,
		enableDrag,
		maxSnapPointPx,
		resolvedDismissThresholdPx,
		resolvedSnapPointsLength,
		sheetRef,
		snapPointTranslatesPx,
		store
	]);
	return {
		onPointerDown,
		onPointerMove,
		onPointerUp,
		onPointerCancel: (0, react.useCallback)((event) => {
			onPointerUp(event);
		}, [onPointerUp])
	};
}
//#endregion
exports.useBottomSheetDrag = useBottomSheetDrag;
