/** @license MIT | @cube-dev/ui-kit v0.0.0-canary-d4ad2ea | Cube Dev Team */
import { NotificationContext, PersistentNotificationsContext, ToastContext, useNotificationContext, useToastContext } from "./NotificationContext.js";
import { OverlayContainer } from "./OverlayContainer.js";
import { useNotificationState } from "./use-notification-state.js";
import { useOverlayTimers } from "./use-overlay-timers.js";
import { usePersistentState } from "./use-persistent-state.js";
import { useToastState } from "./use-toast-state.js";
import { useMemo, useRef } from "react";
import { jsx, jsxs } from "react/jsx-runtime";

//#region src/components/overlays/Notifications/OverlayProvider.tsx
const DEFAULT_MAX_PERSISTENT = 200;
function OverlayProvider({ children, maxPersistentNotifications = DEFAULT_MAX_PERSISTENT }) {
	const timersRef = useRef(null);
	const persistent = usePersistentState(maxPersistentNotifications);
	const toast = useToastState(timersRef);
	const notification = useNotificationState(timersRef, persistent);
	const timers = useOverlayTimers({
		toastsRef: toast.toastsRef,
		notificationsRef: notification.notificationsRef,
		removeToast: toast.removeToast,
		removeNotification: notification.removeNotification
	});
	timersRef.current = timers;
	const toastContextValue = useMemo(() => ({
		addToast: toast.addToast,
		removeToast: toast.removeToast,
		updateToast: toast.updateToast,
		toasts: toast.toasts
	}), [toast.toasts]);
	const notificationContextValue = useMemo(() => ({
		addNotification: notification.addNotification,
		removeNotification: notification.removeNotification,
		updateNotification: notification.updateNotification,
		notifications: notification.notifications,
		addPersistentItem: persistent.addPersistentItem,
		removePersistentItem: persistent.removePersistentItem,
		removePersistentItemsByOwner: persistent.removePersistentItemsByOwner,
		removeByOwner: notification.removeByOwner
	}), [notification.notifications]);
	const persistentContextValue = useMemo(() => ({
		persistentItems: persistent.persistentItems,
		removePersistentItem: persistent.removePersistentItem,
		clearPersistentItems: persistent.clearPersistentItems,
		markAllAsRead: persistent.markAllAsRead
	}), [persistent.persistentItems]);
	return /* @__PURE__ */ jsx(ToastContext.Provider, {
		value: toastContextValue,
		children: /* @__PURE__ */ jsx(NotificationContext.Provider, {
			value: notificationContextValue,
			children: /* @__PURE__ */ jsxs(PersistentNotificationsContext.Provider, {
				value: persistentContextValue,
				children: [children, /* @__PURE__ */ jsx(OverlayContainer, {
					toasts: toast.toasts,
					notifications: notification.notifications,
					onToastExitComplete: toast.finalizeToastRemoval,
					onNotificationExitComplete: notification.finalizeNotificationRemoval,
					onNotificationDismiss: notification.removeNotification,
					onNotificationRestore: notification.restoreNotification,
					onPauseChange: timers.handlePauseChange
				})]
			})
		})
	});
}

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