// SSR-safe localStorage wrapper. Returns null / no-ops when window
// is undefined or access throws (private mode, quota, security).
//
// Caller handles parse / serialize. For object payloads, JSON.stringify
// before write and JSON.parse(read(key) ?? '...') after.

export function read(key: string): string | null {
  if (typeof localStorage === 'undefined') return null;
  try {
    return localStorage.getItem(key);
  } catch {
    return null;
  }
}

export function write(key: string, value: string): void {
  if (typeof localStorage === 'undefined') return;
  try {
    localStorage.setItem(key, value);
  } catch {
    // quota / security — drop silently
  }
}

export function remove(key: string): void {
  if (typeof localStorage === 'undefined') return;
  try {
    localStorage.removeItem(key);
  } catch {
    /* ignore */
  }
}
