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

//#region src/components/overlays/Toast/index.ts
/**
* Declarative Toast component that shows a toast while mounted.
*
* @example
* ```tsx
* // Simple toast (visible while mounted)
* <Toast theme="success">Saved successfully</Toast>
*
* // With description
* <Toast title="Success" description="Changes saved" theme="success" />
* ```
*/
function Toast(props) {
	const { children, ...toastData } = props;
	const { addToast, removeToast, updateToast } = useToastContext();
	const toastIdRef = useRef(null);
	const icon = getThemeIcon(toastData.theme, toastData.icon, toastData.isLoading);
	useEffect(() => {
		toastIdRef.current = addToast({
			...toastData,
			title: toastData.title ?? children,
			icon,
			duration: null
		}, true);
		return () => {
			if (toastIdRef.current != null) {
				removeToast(toastIdRef.current);
				toastIdRef.current = null;
			}
		};
	}, []);
	useEffect(() => {
		if (toastIdRef.current != null) updateToast(toastIdRef.current, {
			...toastData,
			title: toastData.title ?? children,
			icon
		});
	}, [
		children,
		toastData.title,
		toastData.description,
		toastData.theme,
		toastData.icon,
		toastData.isLoading,
		toastData.id,
		icon,
		updateToast
	]);
	return null;
}
/**
* Declarative Progress Toast component.
*
* @example
* ```tsx
* <Toast.Progress
*   {...(isLoading
*     ? { isLoading: true, title: 'Saving...' }
*     : { isLoading: false, title: 'Saved!', theme: 'success' }
*   )}
* />
* ```
*/
function ToastProgress(props) {
	const { children, ...progressOptions } = props;
	useProgressToast({
		...progressOptions,
		title: progressOptions.title ?? children
	});
	return null;
}
Toast.Progress = ToastProgress;

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