/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
import { useCallback, useEffect, useRef } from "react";
//#region src/components/client/BottomSheet/useExpansionMargins.ts
/**
* Manages the `fullWidthAtMaxSnap` feature: animates configured CSS margins to 0
* as the sheet expands from the second-to-last snap point to the max snap point.
*
* Returns a stable `applyExpansionMargins` callback (safe for `useCallback([], [])`)
* and a `thresholdRef` that the caller must set during render after the snap model
* resolves pixel snap points
*/
function useExpansionMargins({ sheetRef, maxSnapPxRef, fullWidthAtMaxSnap, snapPointsLength, isOpen, forceZeroRef }) {
	const thresholdRef = useRef(null);
	const marginsRef = useRef(null);
	const applyExpansionMargins = useCallback((el, currentTranslatePx) => {
		if (forceZeroRef.current) {
			el.style.marginLeft = "0px";
			el.style.marginRight = "0px";
			el.style.marginBottom = "0px";
			return;
		}
		const threshold = thresholdRef.current;
		if (threshold == null) return;
		let margins = marginsRef.current;
		if (!margins) {
			el.style.removeProperty("margin-left");
			el.style.removeProperty("margin-right");
			el.style.removeProperty("margin-bottom");
			const computed = getComputedStyle(el);
			const left = parseFloat(computed.marginLeft) || 0;
			const right = parseFloat(computed.marginRight) || 0;
			const bottom = parseFloat(computed.marginBottom) || 0;
			if (left === 0 && right === 0 && bottom === 0) return;
			margins = {
				left,
				right,
				bottom
			};
			marginsRef.current = margins;
		}
		const maxSnap = maxSnapPxRef.current;
		const range = maxSnap - threshold;
		if (range <= 0) return;
		const rawProgress = (maxSnap - currentTranslatePx - threshold) / range;
		const progress = Math.max(0, Math.min(1, rawProgress));
		if (progress <= 0) {
			marginsRef.current = null;
			el.style.removeProperty("margin-left");
			el.style.removeProperty("margin-right");
			el.style.removeProperty("margin-bottom");
			return;
		}
		el.style.marginLeft = `${margins.left * (1 - progress)}px`;
		el.style.marginRight = `${margins.right * (1 - progress)}px`;
		el.style.marginBottom = `${margins.bottom * (1 - progress)}px`;
	}, [
		forceZeroRef,
		maxSnapPxRef,
		thresholdRef,
		marginsRef
	]);
	useEffect(() => {
		if (isOpen && fullWidthAtMaxSnap && snapPointsLength >= 2) return;
		marginsRef.current = null;
		const el = sheetRef.current;
		if (el) {
			el.style.removeProperty("margin-left");
			el.style.removeProperty("margin-right");
			el.style.removeProperty("margin-bottom");
			if (!isOpen) el.style.removeProperty("height");
		}
	}, [
		isOpen,
		fullWidthAtMaxSnap,
		snapPointsLength,
		sheetRef
	]);
	return {
		applyExpansionMargins,
		thresholdRef
	};
}
//#endregion
export { useExpansionMargins };
