/** @license MIT | @cube-dev/ui-kit v0.142.7 | Cube Dev Team */
import { useEvent } from "../../../_internal/hooks/use-event.js";
import { useEffect, useRef } from "react";

//#region src/components/overlays/Notifications/use-overlay-timers.ts
/**
* Creates a start/clear/pause/resume pair for a single timer category.
* Eliminates duplication between toast and notification timer logic.
*/
function createTimerManager(timersMap, pausedMap, isPausedRef, onTimeout, findItem) {
	const clear = (internalId) => {
		const timer = timersMap.get(internalId);
		if (timer) {
			clearTimeout(timer);
			timersMap.delete(internalId);
		}
		pausedMap.delete(internalId);
	};
	const start = (internalId, id, duration) => {
		clear(internalId);
		pausedMap.set(internalId, {
			remaining: duration,
			startedAt: Date.now()
		});
		if (isPausedRef.current) return;
		const timer = setTimeout(() => {
			onTimeout(id);
		}, duration);
		timersMap.set(internalId, timer);
	};
	const pauseAll = () => {
		timersMap.forEach((timer, internalId) => {
			clearTimeout(timer);
			timersMap.delete(internalId);
			const info = pausedMap.get(internalId);
			if (info) {
				const elapsed = Date.now() - info.startedAt;
				pausedMap.set(internalId, {
					remaining: Math.max(0, info.remaining - elapsed),
					startedAt: Date.now()
				});
			}
		});
	};
	const resumeAll = () => {
		pausedMap.forEach((info, internalId) => {
			const item = findItem(internalId);
			if (info.remaining <= 0) {
				pausedMap.delete(internalId);
				if (item && !item.isExiting) onTimeout(item.id ?? item.internalId);
				return;
			}
			if (item && !item.isExiting) {
				const timer = setTimeout(() => {
					onTimeout(item.id ?? item.internalId);
				}, info.remaining);
				timersMap.set(internalId, timer);
				pausedMap.set(internalId, {
					remaining: info.remaining,
					startedAt: Date.now()
				});
			}
		});
	};
	const clearAll = () => {
		timersMap.forEach((timer) => clearTimeout(timer));
		timersMap.clear();
	};
	return {
		start,
		clear,
		pauseAll,
		resumeAll,
		clearAll
	};
}
function useOverlayTimers(deps) {
	const { toastsRef, notificationsRef, removeToast, removeNotification } = deps;
	const toastTimersMap = useRef(/* @__PURE__ */ new Map()).current;
	const notificationTimersMap = useRef(/* @__PURE__ */ new Map()).current;
	const isPausedRef = useRef(false);
	const pausedMap = useRef(/* @__PURE__ */ new Map()).current;
	const toastManager = useRef(null);
	const notifManager = useRef(null);
	if (!toastManager.current) toastManager.current = createTimerManager(toastTimersMap, pausedMap, isPausedRef, (id) => removeToast(id), (internalId) => toastsRef.current?.find((t) => t.internalId === internalId));
	if (!notifManager.current) notifManager.current = createTimerManager(notificationTimersMap, pausedMap, isPausedRef, (id) => removeNotification(id, "timeout"), (internalId) => notificationsRef.current?.find((n) => n.internalId === internalId));
	const startToastTimer = useEvent((internalId, id, duration) => {
		toastManager.current.start(internalId, id, duration);
	});
	const clearToastTimer = useEvent((internalId) => {
		toastManager.current.clear(internalId);
	});
	const startNotificationTimer = useEvent((internalId, id, duration) => {
		notifManager.current.start(internalId, id, duration);
	});
	const clearNotificationTimer = useEvent((internalId) => {
		notifManager.current.clear(internalId);
	});
	const handlePauseChange = useEvent((paused) => {
		if (paused === isPausedRef.current) return;
		isPausedRef.current = paused;
		if (paused) {
			toastManager.current.pauseAll();
			notifManager.current.pauseAll();
		} else {
			toastManager.current.resumeAll();
			notifManager.current.resumeAll();
		}
	});
	useEffect(() => {
		return () => {
			toastManager.current.clearAll();
			notifManager.current.clearAll();
			pausedMap.clear();
		};
	}, []);
	return {
		startToastTimer,
		clearToastTimer,
		startNotificationTimer,
		clearNotificationTimer,
		handlePauseChange
	};
}

//#endregion
export { useOverlayTimers };
//# sourceMappingURL=use-overlay-timers.js.map