import { STORAGE_KEY_THEME_ACTIVE, STORAGE_KEY_THEME_DRAFT } from '../../../../core/constants';

export interface ThemeStorageSnapshot {
  activeText: string | null;
  draftText: string | null;
}

function readStoredThemeText(key: string): string | null {
  try {
    const raw = localStorage.getItem(key);

    return typeof raw === 'string' && raw.length > 0
      ? raw
      : null;
  } catch {
    return null;
  }
}

function writeStoredThemeText(key: string, value: string): void {
  try {
    localStorage.setItem(key, value);
  } catch {
    /* storage can be unavailable in private contexts */
  }
}

export function loadThemeStorageSnapshot(): ThemeStorageSnapshot {
  return {
    activeText: readStoredThemeText(STORAGE_KEY_THEME_ACTIVE),
    draftText: readStoredThemeText(STORAGE_KEY_THEME_DRAFT)
  };
}

export function saveActiveThemeText(text: string): void {
  writeStoredThemeText(STORAGE_KEY_THEME_ACTIVE, text);
}

export function saveDraftThemeText(text: string): void {
  writeStoredThemeText(STORAGE_KEY_THEME_DRAFT, text);
}
