import { browser } from '$app/environment';

/**
 * Tracks which release-notes version a user has already seen, so the "What's
 * new" dialog shows exactly once per version per user rather than on every
 * login. State is kept client-side in localStorage, keyed per user, so a fresh
 * browser/device will re-show the current version's notes once.
 */
const STORAGE_PREFIX = 'hsf:whats-new:seen';

function storageKey(userKey: string): string {
  return `${STORAGE_PREFIX}:${userKey}`;
}

/**
 * True if this user has already acknowledged the given version. Errs on the
 * side of "seen" when storage is unavailable (SSR, private mode) so we never
 * pop the dialog where we can't record that it was dismissed.
 */
export function hasSeenVersion(userKey: string, version: string): boolean {
  if (!browser) return true;
  try {
    return window.localStorage.getItem(storageKey(userKey)) === version;
  } catch {
    return true;
  }
}

/** Record that this user has seen the given version's release notes. */
export function markVersionSeen(userKey: string, version: string): void {
  if (!browser) return;
  try {
    window.localStorage.setItem(storageKey(userKey), version);
  } catch {
    // A storage failure just means the dialog may reappear next login; harmless.
  }
}
