import * as React from "react";
import { Fragment, jsx } from "react/jsx-runtime";
//#region src/SkeletonGuard.tsx
const DEFAULT_DELAY_MS = 200;
const DEFAULT_MIN_DURATION_MS = 400;
function SkeletonGuard({ pending, children, delayMs = DEFAULT_DELAY_MS, minDurationMs = DEFAULT_MIN_DURATION_MS }) {
	const [visible, setVisible] = React.useState(false);
	const shownAtRef = React.useRef(null);
	React.useEffect(() => {
		if (pending) {
			if (shownAtRef.current !== null) return void 0;
			const delayTimer = globalThis.setTimeout(() => {
				shownAtRef.current = Date.now();
				setVisible(true);
			}, delayMs);
			return () => {
				globalThis.clearTimeout(delayTimer);
			};
		}
		if (shownAtRef.current === null) return;
		const remaining = Math.max(0, minDurationMs - (Date.now() - shownAtRef.current));
		const holdTimer = globalThis.setTimeout(() => {
			shownAtRef.current = null;
			setVisible(false);
		}, remaining);
		return () => {
			globalThis.clearTimeout(holdTimer);
		};
	}, [
		pending,
		delayMs,
		minDurationMs
	]);
	return visible ? /* @__PURE__ */ jsx(Fragment, { children }) : null;
}
//#endregion
export { SkeletonGuard };
