const memory = new Map<string, string>();

export function safeGetItem(key: string) {
  try {
    return localStorage.getItem(key) ?? memory.get(key) ?? "";
  } catch {
    return memory.get(key) ?? "";
  }
}

export function safeSetItem(key: string, value: string) {
  memory.set(key, value);
  try {
    localStorage.setItem(key, value);
  } catch {
    // Keep the in-memory fallback for restricted browsers.
  }
}

export function safeRemoveItem(key: string) {
  memory.delete(key);
  try {
    localStorage.removeItem(key);
  } catch {
    // Ignore storage failures; callers already removed the fallback value.
  }
}
