// Snooze the overdue-tasks popup for a few hours so admins/members aren't nagged on every
// dashboard load. Stored in localStorage keyed by user id — per-browser, per-account.
//
// Used by /admin/dashboard and /user/dashboard. The C# side calls isActive() before deciding
// to render the popup; the popup's "Remind me again in: 1h / 4h / 1 day" buttons call snooze().
window.tmOverdueSnooze = window.tmOverdueSnooze || (function () {
    const key = (uid) => `tm_overdue_snooze_until_${uid || 0}`;

    function isActive(userId) {
        try {
            const raw = localStorage.getItem(key(userId));
            if (!raw) return false;
            const until = parseInt(raw, 10);
            if (isNaN(until)) return false;
            if (Date.now() < until) return true;
            // Expired — clean up so localStorage doesn't accumulate stale keys.
            localStorage.removeItem(key(userId));
            return false;
        } catch {
            return false;
        }
    }

    function snooze(userId, hours) {
        try {
            const until = Date.now() + hours * 60 * 60 * 1000;
            localStorage.setItem(key(userId), String(until));
        } catch { }
        // Close the SweetAlert popup if it's open. Falls back gracefully if the global isn't there.
        try { if (window.Swal && typeof window.Swal.close === 'function') window.Swal.close(); } catch { }
    }

    return { isActive, snooze };
})();
