"use client";
/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
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);

//#region src/components/client/BottomSheet/useBottomSheetSnapModel.ts
const DEFAULT_SNAP_POINT = "40%";
const OPEN_SHEET_ENTER_DELAY_MS = 50;
function useBottomSheetSnapModel({ isOpen, snapPointsProp, height, defaultSnapPointIndex, snapPointIndexProp, onSnapPointChange, viewportHeightPx, maxHeightPx, setTranslateAnimated, isDraggingRef }) {
	const openTimeRef = (0, react.useRef)(null);
	const lastTimeDragPreventedRef = (0, react.useRef)(null);
	const openEnterTimeoutRef = (0, react.useRef)(null);
	const isEnteringRef = (0, react.useRef)(false);
	const resolvedSnapPoints = (0, react.useMemo)(() => {
		if (snapPointsProp?.length) return snapPointsProp;
		if (height !== void 0) return [height];
		return [DEFAULT_SNAP_POINT];
	}, [height, snapPointsProp]);
	const snapPointsPx = (0, react.useMemo)(() => {
		return resolvedSnapPoints.map((sp) => (0, lodash_clamp_js.default)(require_components_client_BottomSheet_utils.resolveHeightToPx(sp, viewportHeightPx), 0, maxHeightPx));
	}, [
		maxHeightPx,
		resolvedSnapPoints,
		viewportHeightPx
	]);
	const maxSnapPointPx = (0, react.useMemo)(() => Math.max(0, ...snapPointsPx), [snapPointsPx]);
	const snapPointTranslatesPx = (0, react.useMemo)(() => {
		return snapPointsPx.map((h) => maxSnapPointPx - h);
	}, [maxSnapPointPx, snapPointsPx]);
	const isSnapPointControlled = snapPointIndexProp !== void 0;
	const [uncontrolledSnapIndex, setUncontrolledSnapIndex] = (0, react.useState)(() => require_components_client_BottomSheet_utils.clampIndex(defaultSnapPointIndex, resolvedSnapPoints.length));
	const activeSnapIndex = require_components_client_BottomSheet_utils.clampIndex(isSnapPointControlled ? snapPointIndexProp ?? 0 : uncontrolledSnapIndex, resolvedSnapPoints.length);
	const activeTranslatePx = snapPointTranslatesPx[activeSnapIndex] ?? maxSnapPointPx;
	const [restTranslatePx, setRestTranslatePx] = (0, react.useState)(() => maxSnapPointPx);
	const lastEmittedTranslateRef = (0, react.useRef)(null);
	const emitSnapPointChange = (0, react.useCallback)((nextIndex) => {
		const clamped = require_components_client_BottomSheet_utils.clampIndex(nextIndex, resolvedSnapPoints.length);
		const nextTranslatePx = snapPointTranslatesPx[clamped] ?? maxSnapPointPx;
		onSnapPointChange?.(clamped, resolvedSnapPoints[clamped]);
		if (!isSnapPointControlled) setUncontrolledSnapIndex(clamped);
		setRestTranslatePx(nextTranslatePx);
		lastEmittedTranslateRef.current = nextTranslatePx;
		setTranslateAnimated(nextTranslatePx);
	}, [
		isSnapPointControlled,
		maxSnapPointPx,
		onSnapPointChange,
		resolvedSnapPoints,
		setTranslateAnimated,
		snapPointTranslatesPx
	]);
	(0, react.useEffect)(() => {
		const wasOpen = openTimeRef.current !== null;
		if (!isOpen) {
			if (openEnterTimeoutRef.current !== null) {
				window.clearTimeout(openEnterTimeoutRef.current);
				openEnterTimeoutRef.current = null;
			}
			isEnteringRef.current = false;
			openTimeRef.current = null;
			setRestTranslatePx(maxSnapPointPx);
			return;
		}
		if (wasOpen) return;
		isEnteringRef.current = true;
		openTimeRef.current = Date.now();
		const initialSnapIndex = isSnapPointControlled ? activeSnapIndex : require_components_client_BottomSheet_utils.clampIndex(defaultSnapPointIndex, resolvedSnapPoints.length);
		if (!isSnapPointControlled) setUncontrolledSnapIndex(initialSnapIndex);
		setRestTranslatePx(maxSnapPointPx);
		const initialTranslatePx = snapPointTranslatesPx[initialSnapIndex] ?? maxSnapPointPx;
		openEnterTimeoutRef.current = window.setTimeout(() => {
			isEnteringRef.current = false;
			setRestTranslatePx(initialTranslatePx);
			setTranslateAnimated(initialTranslatePx);
			openEnterTimeoutRef.current = null;
		}, OPEN_SHEET_ENTER_DELAY_MS);
	}, [
		activeSnapIndex,
		defaultSnapPointIndex,
		isOpen,
		isSnapPointControlled,
		maxSnapPointPx,
		resolvedSnapPoints.length,
		setTranslateAnimated,
		snapPointTranslatesPx
	]);
	(0, react.useEffect)(() => {
		return () => {
			if (openEnterTimeoutRef.current !== null) {
				window.clearTimeout(openEnterTimeoutRef.current);
				openEnterTimeoutRef.current = null;
			}
		};
	}, []);
	(0, react.useEffect)(() => {
		if (!isOpen || isEnteringRef.current || isDraggingRef.current) return;
		setRestTranslatePx(activeTranslatePx);
		if (lastEmittedTranslateRef.current === activeTranslatePx) {
			lastEmittedTranslateRef.current = null;
			return;
		}
		setTranslateAnimated(activeTranslatePx);
	}, [
		activeTranslatePx,
		isDraggingRef,
		isOpen,
		setTranslateAnimated
	]);
	(0, react.useEffect)(() => {
		if (!isOpen) return;
		if (activeSnapIndex === resolvedSnapPoints.length - 1) openTimeRef.current = Date.now();
	}, [
		activeSnapIndex,
		isOpen,
		resolvedSnapPoints.length
	]);
	return {
		resolvedSnapPoints,
		snapPointsPx,
		maxSnapPointPx,
		snapPointTranslatesPx,
		activeSnapIndex,
		restTranslatePx,
		setRestTranslatePx,
		isSnapPointControlled,
		emitSnapPointChange,
		openTimeRef,
		lastTimeDragPreventedRef,
		isEnteringRef
	};
}

//#endregion
exports.useBottomSheetSnapModel = useBottomSheetSnapModel;