let currentTheme = 'system';
const themeListeners = new Set();
let mediaQueryCleanup = null;
/**
 * Get the currently applied theme mode
 */
export function getTheme() {
    return currentTheme;
}
/**
 * Get the resolved theme (light or dark) based on current mode
 */
export function getResolvedTheme() {
    if (currentTheme === 'system') {
        return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }
    return currentTheme;
}
/**
 * Apply a theme mode to the document
 * @param theme - 'light', 'dark', or 'system' (follows OS preference)
 */
export function applyTheme(theme) {
    // Clean up previous system listener
    if (mediaQueryCleanup) {
        mediaQueryCleanup();
        mediaQueryCleanup = null;
    }
    currentTheme = theme;
    if (theme === 'system') {
        // Listen for system preference changes
        const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
        const handleChange = () => {
            const resolved = mediaQuery.matches ? 'dark' : 'light';
            document.documentElement.setAttribute('data-theme', resolved);
            notifyListeners('system', resolved);
        };
        mediaQuery.addEventListener('change', handleChange);
        mediaQueryCleanup = () => mediaQuery.removeEventListener('change', handleChange);
        // Apply current system preference
        const resolved = mediaQuery.matches ? 'dark' : 'light';
        document.documentElement.setAttribute('data-theme', resolved);
        notifyListeners('system', resolved);
    }
    else {
        document.documentElement.setAttribute('data-theme', theme);
        notifyListeners(theme, theme);
    }
}
/**
 * Subscribe to theme changes
 * @param callback - Function called when theme changes
 * @returns Unsubscribe function
 */
export function onThemeChange(callback) {
    themeListeners.add(callback);
    return () => themeListeners.delete(callback);
}
function notifyListeners(theme, resolved) {
    themeListeners.forEach((callback) => callback(theme, resolved));
}
/**
 * Toggle between light and dark themes
 * If currently on 'system', will switch to the opposite of current resolved theme
 */
export function toggleTheme() {
    const resolved = getResolvedTheme();
    applyTheme(resolved === 'light' ? 'dark' : 'light');
}
