import { ref, watch } from 'vue' export type ThemeMode = 'light' | 'dark' | 'system' const STORAGE_KEY = 'WeKnora_theme' // Shared reactive state across all consumers const currentTheme = ref( (localStorage.getItem(STORAGE_KEY) as ThemeMode) || 'light' ) function getSystemTheme(): 'light' | 'dark' { return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light' } function applyTheme(mode: ThemeMode) { const effective = mode === 'system' ? getSystemTheme() : mode document.documentElement.setAttribute('theme-mode', effective) } export function useTheme() { function setTheme(mode: ThemeMode) { currentTheme.value = mode localStorage.setItem(STORAGE_KEY, mode) applyTheme(mode) } return { currentTheme, setTheme } } /** Call once in main.ts to initialise theme and listen for OS changes. */ export function initTheme() { const saved = (localStorage.getItem(STORAGE_KEY) as ThemeMode) || 'light' currentTheme.value = saved applyTheme(saved) // React to OS theme changes when user chose "system" window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { if (currentTheme.value === 'system') { applyTheme('system') } }) }