import { sessionStorageKeyConstants } from "@/constants/session-storage"; const recoveryKey = sessionStorageKeyConstants.BEFORE_EACH_ERROR_RECOVERY_COUNT; /** Used when sessionStorage is missing, throws, or does not persist (e.g. some bots). */ let inMemoryRecoveryCount = 0; export function getBeforeEachErrorRecoveryCount() { try { const stored = window.sessionStorage?.getItem(recoveryKey); if (stored !== null && stored !== undefined) { return Number(stored) || 0; } } catch { // sessionStorage unavailable; use in-memory fallback } return inMemoryRecoveryCount; } export function incrementBeforeEachErrorRecoveryCount() { const nextCount = getBeforeEachErrorRecoveryCount() + 1; inMemoryRecoveryCount = nextCount; try { window.sessionStorage?.setItem(recoveryKey, String(nextCount)); } catch { // in-memory count already updated } return nextCount; } export function clearBeforeEachErrorRecoveryCount() { inMemoryRecoveryCount = 0; try { window.sessionStorage?.removeItem(recoveryKey); } catch { // in-memory count already cleared } } export function hasBeforeEachErrorRecoveryBeenAttempted() { return getBeforeEachErrorRecoveryCount() >= 1; } /** Test-only: resets in-memory and sessionStorage recovery state between unit tests. */ export function resetBeforeEachErrorRecoveryStateForTests() { clearBeforeEachErrorRecoveryCount(); }