/**
 * Cross-page transient prefill store.
 *
 * Mirrors Angular's `OnlineBoardFiltersStateService` /
 * `ScheduleFiltersStateService` — singleton state used to hand
 * popular-request data from one page to another without polluting the
 * URL with query strings. Reload clears it (matches Angular's behavior:
 * a fresh navigation to the page bypasses the service-held state).
 *
 * sessionStorage is the host: same-tab persistence, gone on tab close,
 * works around react-router v6's same-path navigation collapsing
 * `state` updates without re-rendering useLocation consumers. Access
 * goes through `sessionStore` so the no-restricted-globals rule stays
 * unviolated.
 */

import { sessionStore } from "@/shared/storage.js";

const KEY_PREFIX = "afl-prefill:";

export function writeTransientPrefill<T>(slot: string, value: T): void {
  sessionStore.setRaw(KEY_PREFIX + slot, JSON.stringify(value));
}

export function readAndClearTransientPrefill<T>(slot: string): T | null {
  const key = KEY_PREFIX + slot;
  const raw = sessionStore.getRaw(key);
  if (raw === null) return null;
  sessionStore.delete(key);
  try {
    return JSON.parse(raw) as T;
  } catch {
    return null;
  }
}
