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

import { useRef, useState } from 'react';
import { useEvent } from '../../../_internal/index.js';
// ─── Hook ────────────────────────────────────────────────────────────
export function usePersistentState(maxItems) {
    const [persistentItems, setPersistentItems] = useState([]);
    // Tracks IDs that have been moved to the persistent list at least once.
    // Used to skip the overlay when the same id reappears.
    const dismissedPersistentIdsRef = useRef(new Set());
    // Tracks IDs that were explicitly removed from the persistent list by the
    // user. These should be completely ignored on subsequent triggers.
    const fullyDismissedIdsRef = useRef(new Set());
    const addPersistentItem = useEvent((item) => {
        // If the user already dismissed this item from the persistent list, don't re-add it.
        if (fullyDismissedIdsRef.current.has(item.id))
            return;
        dismissedPersistentIdsRef.current.add(item.id);
        setPersistentItems((prev) => {
            // Upsert by id
            const existingIndex = prev.findIndex((i) => i.id === item.id);
            let newItems;
            if (existingIndex !== -1) {
                newItems = [...prev];
                // Updated items are marked unread again
                newItems[existingIndex] = {
                    ...newItems[existingIndex],
                    ...item,
                    isRead: false,
                };
            }
            else {
                newItems = [{ ...item, isRead: item.isRead ?? false }, ...prev];
            }
            // Enforce max cap
            if (newItems.length > maxItems) {
                newItems = newItems.slice(0, maxItems);
            }
            // Sort newest first
            newItems.sort((a, b) => b.createdAt - a.createdAt);
            return newItems;
        });
    });
    const removePersistentItem = useEvent((id) => {
        fullyDismissedIdsRef.current.add(id);
        setPersistentItems((prev) => prev.filter((i) => i.id !== id));
    });
    const removePersistentItemsByOwner = useEvent((ownerId) => {
        setPersistentItems((prev) => prev.filter((i) => i.ownerId !== ownerId));
    });
    const clearPersistentItems = useEvent(() => {
        setPersistentItems([]);
    });
    const markAllAsRead = useEvent(() => {
        setPersistentItems((prev) => {
            const hasUnread = prev.some((i) => !i.isRead);
            if (!hasUnread)
                return prev;
            return prev.map((i) => (i.isRead ? i : { ...i, isRead: true }));
        });
    });
    const hasDismissedPersistentId = useEvent((id) => {
        return dismissedPersistentIdsRef.current.has(id);
    });
    const isFullyDismissedId = useEvent((id) => {
        return fullyDismissedIdsRef.current.has(id);
    });
    return {
        persistentItems,
        addPersistentItem,
        removePersistentItem,
        removePersistentItemsByOwner,
        clearPersistentItems,
        markAllAsRead,
        hasDismissedPersistentId,
        isFullyDismissedId,
    };
}

        