import { useEffect, useRef, useState } from "react";
//#region src/plus/pisellReservation/hooks/useReservationLoadingShownDebounced.ts
/**
* RecordBoard 的 loading：仅当 `loading` 持续超过 `reservationConstants` 中的延迟毫秒数才置为 true，
* 短时请求不闪遮罩；`loading` 为 false 时立即关闭。
*/
function useReservationLoadingShownDebounced(loading) {
	const [loadingShown, setLoadingShown] = useState(false);
	const loadingShowTimerRef = useRef(null);
	useEffect(() => {
		if (loadingShowTimerRef.current != null) {
			window.clearTimeout(loadingShowTimerRef.current);
			loadingShowTimerRef.current = null;
		}
		if (loading) loadingShowTimerRef.current = window.setTimeout(() => {
			loadingShowTimerRef.current = null;
			setLoadingShown(true);
		}, 200);
		else setLoadingShown(false);
		return () => {
			if (loadingShowTimerRef.current != null) {
				window.clearTimeout(loadingShowTimerRef.current);
				loadingShowTimerRef.current = null;
			}
		};
	}, [loading]);
	return loadingShown;
}
//#endregion
export { useReservationLoadingShownDebounced };
