/**
 * @license MIT
 * author: Cube Dev Team
 * @cube-dev/ui-kit v0.0.0-canary-3803e7e
 * Released under the MIT license.
 */

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useMemo, useRef } from 'react';
import { NotificationContext, PersistentNotificationsContext, ToastContext, } 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';
// ─── Re-export context hooks for consumers ───────────────────────────
export { useToastContext, useNotificationContext } from './NotificationContext.js';
// ─── Constants ───────────────────────────────────────────────────────
const DEFAULT_MAX_PERSISTENT = 200;
// ─── OverlayProvider ─────────────────────────────────────────────────
export function OverlayProvider({ children, maxPersistentNotifications = DEFAULT_MAX_PERSISTENT, }) {
    // Shared ref that breaks the circular dependency between state hooks and timers.
    // State hooks need timer methods (start/clear), timer hooks need remove methods.
    // All consumers access the ref only inside useEvent/callbacks (never during
    // render), so it is guaranteed to be populated before first use.
    const timersRef = useRef(null);
    // 1. Persistent state (no dependencies on other hooks)
    const persistent = usePersistentState(maxPersistentNotifications);
    // 2. Toast state — accesses timers through the shared ref
    const toast = useToastState(timersRef);
    // 3. Notification state — accesses timers through the shared ref
    const notification = useNotificationState(timersRef, persistent);
    // 4. Create timers and populate the ref
    const timers = useOverlayTimers({
        toastsRef: toast.toastsRef,
        notificationsRef: notification.notificationsRef,
        removeToast: toast.removeToast,
        removeNotification: notification.removeNotification,
    });
    timersRef.current = timers;
    // ─── Context Values ───────────────────────────────────────────────
    // Only include actually-changing values in deps. All useEvent-based
    // callbacks are referentially stable and omitted.
    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 (_jsx(ToastContext.Provider, { value: toastContextValue, children: _jsx(NotificationContext.Provider, { value: notificationContextValue, children: _jsxs(PersistentNotificationsContext.Provider, { value: persistentContextValue, children: [children, _jsx(OverlayContainer, { toasts: toast.toasts, notifications: notification.notifications, onToastExitComplete: toast.finalizeToastRemoval, onNotificationExitComplete: notification.finalizeNotificationRemoval, onNotificationDismiss: notification.removeNotification, onPauseChange: timers.handlePauseChange })] }) }) }));
}

        