// Track which folders the current user has opened, and when.
// Used to show a "Nouveau" badge when a folder was updated after we last
// viewed it. Stored in localStorage to keep it simple and per-device.
const KEY = 'mytruck_seen_folders';

function readMap() {
    try {
        const raw = localStorage.getItem(KEY);
        if (!raw) return {};
        const obj = JSON.parse(raw);
        return obj && typeof obj === 'object' ? obj : {};
    } catch (_) {
        return {};
    }
}

function writeMap(m) {
    try { localStorage.setItem(KEY, JSON.stringify(m)); } catch (_) {}
}

export function markFolderSeen(folderId) {
    if (!folderId) return;
    const m = readMap();
    m[String(folderId)] = Date.now();
    writeMap(m);
}

export function getSeenAt(folderId) {
    if (!folderId) return 0;
    return readMap()[String(folderId)] || 0;
}

export function isFolderUnread(folder) {
    if (!folder || !folder.updated_at) return false;
    const seen = getSeenAt(folder.id);
    if (!seen) return true;
    return new Date(folder.updated_at).getTime() > seen;
}
