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

import { useEffect, useMemo, useRef, useState } from 'react';
import { useEvent } from '../../../../_internal/index.js';
const DISMISS_EVENT_NAME = 'cube:notification:dismiss';
export function useNotifications(rootRef) {
    const idPrefix = useMemo(() => new Date().getTime(), []);
    const idRef = useRef(0);
    const listeners = useRef([]);
    const [notifications, setNotifications] = useState(new Map());
    const addToast = Object.assign(useEvent((props) => {
        const nextID = idRef.current++;
        const { id = `${idPrefix}_${nextID}`, duration, isDismissible = true, ...rest } = props;
        setNotifications((toasts) => {
            const newToasts = new Map(toasts);
            newToasts.set(id, {
                id,
                isDismissible,
                duration: duration ?? (isDismissible ? 5_000 : null),
                ...rest,
            });
            return newToasts;
        });
        return {
            id,
            remove: () => removeToast(id),
            update: (props) => updateToast(id, props),
        };
    }), {
        success: (props) => addToast({ type: 'success', ...props }),
        danger: (props) => addToast({ type: 'danger', ...props }),
        attention: (props) => addToast({ type: 'attention', ...props }),
    });
    const updateToast = useEvent((id, props) => {
        setNotifications((toasts) => {
            const currentToast = toasts.get(id);
            if (currentToast) {
                const newToasts = new Map(toasts);
                newToasts.set(id, {
                    ...currentToast,
                    ...props,
                });
                return newToasts;
            }
            return toasts;
        });
    });
    const removeToast = useEvent((id) => {
        setNotifications((toasts) => {
            if (toasts.has(id)) {
                const newToasts = new Map(toasts);
                newToasts.delete(id);
                return newToasts;
            }
            return toasts;
        });
    });
    const onDismissNotification = useEvent((id) => {
        const toast = notifications.get(id);
        if (toast == null) {
            return;
        }
        if (toast?.putNotificationInDropdownOnDismiss !== false) {
            rootRef?.current?.dispatchEvent(new CustomEvent(DISMISS_EVENT_NAME, { detail: toast }));
        }
    });
    const addOnDismissListener = useEvent((listener) => {
        const callback = (e) => listener(e.detail);
        const unsub = () => rootRef?.current?.removeEventListener(DISMISS_EVENT_NAME, callback);
        listeners.current.push(unsub);
        rootRef?.current?.addEventListener(DISMISS_EVENT_NAME, callback);
        return unsub;
    });
    useEffect(() => () => listeners.current.forEach((cb) => cb()), []);
    const api = useMemo(() => ({ notify: addToast, update: updateToast, remove: removeToast }), 
    // All deps are stable since we use `useEvent` hook
    []);
    return {
        api,
        notifications: useMemo(() => [...notifications.values()], [notifications]),
        onDismissNotification,
        addOnDismissListener,
    };
}

        