require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
//#region src/components/PisellProcedure/hooks/useSidebarHeight.ts
/**
* 使用 Sidebar 高度的 Hook
*
* @description 计算并管理 Sidebar 的高度，确保在尺寸稳定后才应用
*/
const useSidebarHeight = (isSizeStable, sizeValues) => {
	const [siderHeight, setSiderHeight] = (0, react.useState)(void 0);
	const hasInitializedRef = (0, react.useRef)(false);
	const rafIdRef = (0, react.useRef)(null);
	const stableCheckCountRef = (0, react.useRef)(0);
	const previousSiderHeightRef = (0, react.useRef)(void 0);
	const sizeValuesRef = (0, react.useRef)(sizeValues);
	(0, react.useEffect)(() => {
		sizeValuesRef.current = sizeValues;
	}, [sizeValues]);
	/**
	* 获取 body 元素的 CSS transform Y 轴缩放比例
	* @returns scaleY 缩放比例
	*/
	const getBodyScaleY = (0, react.useCallback)(() => {
		try {
			if (typeof window === "undefined" || typeof document === "undefined") return 1;
			const bodyTransform = getComputedStyle(document.body).transform;
			let scaleY = 1;
			if (bodyTransform !== "none") scaleY = new DOMMatrix(bodyTransform).d;
			return scaleY;
		} catch (error) {
			return 1;
		}
	}, []);
	/**
	* 计算高度的函数
	* @description 计算时会考虑 body 上的 CSS transform 缩放，返回缩放校正后的实际逻辑高度
	*/
	const calculateHeight = (0, react.useCallback)(() => {
		const { containerSelector, containerHeight, headerHeight, footerHeight } = sizeValuesRef.current;
		let baseHeight;
		if (containerSelector && containerSelector.trim() && containerHeight && containerHeight > 0) baseHeight = containerHeight;
		else baseHeight = typeof window !== "undefined" ? window.innerHeight : 0;
		const scaledHeight = (baseHeight - (headerHeight || 0) - footerHeight - 32) / getBodyScaleY();
		return Math.round(scaledHeight * 100) / 100;
	}, [sizeValuesRef.current, getBodyScaleY]);
	(0, react.useEffect)(() => {
		if (typeof window === "undefined" || typeof document === "undefined") return;
		if (!isSizeStable) {
			hasInitializedRef.current = false;
			stableCheckCountRef.current = 0;
			if (rafIdRef.current !== null) {
				cancelAnimationFrame(rafIdRef.current);
				rafIdRef.current = null;
			}
			return;
		}
		if (rafIdRef.current !== null) cancelAnimationFrame(rafIdRef.current);
		/**
		* 稳定性检查函数：连续3次检查尺寸都稳定才认为真正稳定
		*/
		const checkStability = () => {
			if (!isSizeStable) {
				stableCheckCountRef.current = 0;
				return;
			}
			const calculatedHeight = calculateHeight();
			if (calculatedHeight <= 0) {
				stableCheckCountRef.current = 0;
				return;
			}
			const previousHeight = previousSiderHeightRef.current;
			if (hasInitializedRef.current && previousHeight !== void 0 && Math.abs(calculatedHeight - previousHeight) < 10) return;
			stableCheckCountRef.current += 1;
			if (stableCheckCountRef.current >= 3) {
				previousSiderHeightRef.current = calculatedHeight;
				setSiderHeight(calculatedHeight);
				hasInitializedRef.current = true;
				stableCheckCountRef.current = 0;
			} else rafIdRef.current = requestAnimationFrame(checkStability);
		};
		rafIdRef.current = requestAnimationFrame(checkStability);
		return () => {
			if (rafIdRef.current !== null) {
				cancelAnimationFrame(rafIdRef.current);
				rafIdRef.current = null;
			}
		};
	}, [isSizeStable, calculateHeight]);
	return siderHeight;
};
//#endregion
exports.useSidebarHeight = useSidebarHeight;
