/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-f1f3d22 | Cube Dev Team */
import { useToastContext } from "./ToastProvider.js";
import { getThemeIcon } from "./useToast.js";
import { useEffect, useRef } from "react";

//#region src/components/overlays/Toast/useProgressToast.ts
const RESULT_DURATION = 3e3;
/**
* Check if the options represent an "empty" value (null, undefined, false, or empty object).
* When empty, the hook should immediately remove any existing toast.
*/
function isEmptyOptions(options) {
	if (options == null || options === false) return true;
	if (typeof options === "object" && Object.keys(options).length === 0) return true;
	return false;
}
function getStringValue(value) {
	return typeof value === "string" ? value : void 0;
}
/**
* Hook to display a progress toast that persists while loading.
*
* @example
* ```tsx
* useProgressToast(
*   isLoading
*     ? { isLoading: true, title: 'Saving...', icon: <Spinner /> }
*     : { isLoading: false, title: 'Saved!', icon: <IconCheck />, theme: 'success' }
* );
*
* // Or with error handling:
* useProgressToast(
*   isLoading
*     ? { isLoading: true, title: 'Saving...', icon: <Spinner /> }
*     : isError
*       ? { isLoading: false, title: 'Error', description: errorMessage, theme: 'danger' }
*       : { isLoading: false, title: 'Saved!', theme: 'success' }
* );
*
* // Pass empty value to immediately remove any existing toast:
* useProgressToast(shouldShow ? { isLoading: true, title: 'Loading...' } : null);
* ```
*/
function useProgressToast(options) {
	const { addToast, removeToast } = useToastContext();
	const toastIdRef = useRef(null);
	const wasLoadingRef = useRef(null);
	const hideTimerRef = useRef(null);
	const isFirstRenderRef = useRef(true);
	const optionsRef = useRef(options);
	const wasEmptyRef = useRef(false);
	const prevThemeRef = useRef(void 0);
	const prevTitleRef = useRef(void 0);
	const prevDescriptionRef = useRef(void 0);
	optionsRef.current = options;
	const clearHideTimer = () => {
		if (hideTimerRef.current) {
			clearTimeout(hideTimerRef.current);
			hideTimerRef.current = null;
		}
	};
	const isEmpty = isEmptyOptions(options);
	const isLoading = isEmpty ? false : options.isLoading;
	const currentTheme = isEmpty ? void 0 : options.theme;
	const currentTitle = isEmpty ? void 0 : getStringValue(options.title);
	const currentDescription = isEmpty ? void 0 : getStringValue(options.description);
	useEffect(() => {
		const wasLoading = wasLoadingRef.current;
		const wasEmpty = wasEmptyRef.current;
		const isFirstRender = isFirstRenderRef.current;
		const currentOptions = optionsRef.current;
		if (isEmptyOptions(currentOptions)) {
			clearHideTimer();
			if (toastIdRef.current != null) {
				removeToast(toastIdRef.current);
				toastIdRef.current = null;
			}
			wasLoadingRef.current = null;
			wasEmptyRef.current = true;
			isFirstRenderRef.current = false;
			prevThemeRef.current = void 0;
			prevTitleRef.current = void 0;
			prevDescriptionRef.current = void 0;
			return;
		}
		const { isLoading: currentIsLoading, ...currentToastData } = currentOptions;
		const themeChanged = currentTheme !== prevThemeRef.current;
		const titleChanged = currentTitle !== prevTitleRef.current;
		const descriptionChanged = currentDescription !== prevDescriptionRef.current;
		const dataChanged = themeChanged || titleChanged || descriptionChanged;
		const showToast = () => {
			if (toastIdRef.current != null) removeToast(toastIdRef.current);
			const icon = getThemeIcon(currentToastData.theme, currentToastData.icon, currentIsLoading);
			toastIdRef.current = addToast({
				...currentToastData,
				icon,
				isLoading: currentIsLoading,
				duration: null
			}, true);
		};
		const showToastWithTimer = () => {
			showToast();
			hideTimerRef.current = setTimeout(() => {
				if (toastIdRef.current != null) {
					removeToast(toastIdRef.current);
					toastIdRef.current = null;
				}
			}, RESULT_DURATION);
		};
		if (currentIsLoading) {
			clearHideTimer();
			if (toastIdRef.current == null) showToast();
			else if (dataChanged) showToast();
		} else if ((wasLoading === true || wasEmpty) && !currentIsLoading) {
			clearHideTimer();
			if (currentTitle) showToastWithTimer();
			else if (toastIdRef.current != null) {
				removeToast(toastIdRef.current);
				toastIdRef.current = null;
			}
		} else if (!currentIsLoading && !isFirstRender && dataChanged) {
			if (currentTitle) {
				clearHideTimer();
				showToastWithTimer();
			} else if (toastIdRef.current != null) {
				clearHideTimer();
				removeToast(toastIdRef.current);
				toastIdRef.current = null;
			}
		}
		wasLoadingRef.current = currentIsLoading;
		wasEmptyRef.current = false;
		isFirstRenderRef.current = false;
		prevThemeRef.current = currentTheme;
		prevTitleRef.current = currentTitle;
		prevDescriptionRef.current = currentDescription;
	}, [
		isEmpty,
		isLoading,
		currentTheme,
		currentTitle,
		currentDescription,
		addToast,
		removeToast
	]);
	useEffect(() => {
		return () => {
			clearHideTimer();
			if (toastIdRef.current != null) {
				removeToast(toastIdRef.current);
				toastIdRef.current = null;
			}
		};
	}, [removeToast]);
}

//#endregion
export { useProgressToast };
//# sourceMappingURL=useProgressToast.js.map