{"version":3,"file":"themeStore.cjs","names":["Store","DEFAULT_THEME","THEME_STORAGE_KEY","BUILTIN_THEMES","pluginThemes","parseThemeConfig","builtinTheme","readThemePreference"],"sources":["../../../src/web/stores/themeStore.ts"],"sourcesContent":["import {\n  applyTheme,\n  BUILTIN_THEMES,\n  builtinTheme,\n  DEFAULT_THEME,\n  parseThemeConfig,\n  readThemePreference,\n  THEME_STORAGE_KEY,\n  type ThemeConfig,\n  writeThemePreference,\n} from '@agimon-ai/po-web-components/theme';\nimport { Store } from '@tanstack/store';\n\nimport { pluginThemes } from '../lib/pluginRegistry';\n\nexport interface ThemeState {\n  /** The name of the theme on the page. */\n  name: string;\n}\n\nexport const themeStore = new Store<ThemeState>({ name: DEFAULT_THEME.name });\n\n/**\n * A copy of the chosen plugin theme, so the first paint can show it before the\n * person's plugins have loaded. It is read back from localStorage, so it is\n * validated like any theme file before it touches the page.\n */\nconst SNAPSHOT_KEY = `${THEME_STORAGE_KEY}.snapshot`;\n\n/** The themes the appearance page offers: the shipped ones, then those the person's plugins add. */\nexport function availableThemes(): readonly ThemeConfig[] {\n  return [...BUILTIN_THEMES, ...pluginThemes().map((entry) => entry.theme)];\n}\n\nfunction pluginTheme(name: string): ThemeConfig | undefined {\n  return pluginThemes().find((entry) => entry.theme.name === name)?.theme;\n}\n\nfunction storage(): Storage | undefined {\n  try {\n    return window.localStorage;\n  } catch {\n    return undefined;\n  }\n}\n\nfunction readSnapshot(name: string): ThemeConfig | undefined {\n  try {\n    const raw = storage()?.getItem(SNAPSHOT_KEY);\n    const theme = raw ? parseThemeConfig(JSON.parse(raw)) : null;\n    return theme?.name === name ? theme : undefined;\n  } catch {\n    return undefined;\n  }\n}\n\n/** Remembers a theme by name, plus a validated copy when only a plugin can supply it. */\nfunction remember(theme: ThemeConfig | null): void {\n  writeThemePreference(storage(), theme?.name ?? null);\n  try {\n    if (theme === null || builtinTheme(theme.name)) storage()?.removeItem(SNAPSHOT_KEY);\n    else storage()?.setItem(SNAPSHOT_KEY, JSON.stringify(theme));\n  } catch (error) {\n    // Private mode or a full quota: the theme still applies now, but the next first paint shows the default.\n    console.warn('Po could not remember the theme snapshot', error);\n  }\n}\n\nfunction show(theme: ThemeConfig): void {\n  applyTheme(theme, document.documentElement);\n  themeStore.setState(() => ({ name: theme.name }));\n}\n\n/**\n * Puts the remembered theme on the page. Called before the first render so\n * the cockpit never flashes the default. A plugin theme comes from its\n * snapshot until the plugins load; a stored name nothing can supply falls back\n * to the default and is forgotten.\n */\nexport function applyStoredTheme(): void {\n  const stored = readThemePreference(storage());\n  const theme = (stored === null ? undefined : (builtinTheme(stored) ?? readSnapshot(stored))) ?? DEFAULT_THEME;\n  if (stored !== null && theme.name !== stored) remember(null);\n  show(theme);\n}\n\n/**\n * Settles a remembered plugin theme once the global plugins are installed: the\n * plugin's current colours replace the snapshot, and a theme its plugin no\n * longer ships falls back to the default and is forgotten.\n */\nexport function reconcileStoredTheme(): void {\n  const stored = readThemePreference(storage());\n  if (stored === null || builtinTheme(stored)) return;\n  const theme = pluginTheme(stored);\n  remember(theme ?? null);\n  show(theme ?? DEFAULT_THEME);\n}\n\n/** Switches the page to a shipped or plugin theme and remembers it. */\nexport function selectTheme(name: string): void {\n  const theme = builtinTheme(name) ?? pluginTheme(name);\n  if (theme === undefined) return;\n  show(theme);\n  remember(theme);\n}\n"],"mappings":";;;AAoBA,MAAa,aAAa,K,yBAAIA,EAAAA,CAAAA,MAAkB,EAAE,MAAMC,mCAAAA,cAAc,KAAK,CAAC;;;;;;AAO5E,MAAM,eAAe,GAAGC,mCAAAA,kBAAkB;;AAG1C,SAAgB,kBAA0C;CACxD,OAAO,CAAC,GAAGC,mCAAAA,gBAAgB,GAAGC,uBAAAA,aAAa,CAAC,CAAC,KAAK,UAAU,MAAM,KAAK,CAAC;AAC1E;AAEA,SAAS,YAAY,MAAuC;CAC1D,OAAOA,uBAAAA,aAAa,CAAC,CAAC,MAAM,UAAU,MAAM,MAAM,SAAS,IAAI,CAAC,EAAE;A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