// ==========================================
// Chat Switchboard — SDK: Theme Control
// ==========================================
// Theme management: localStorage + data-theme + system pref.
// Port of Theme from ui-primitives.js as ES module.
//
// Factory: createTheme(emitFn)
// ==========================================

const _key = 'switchboard_theme';

/**
 * Create the theme module.
 *
 * @param {Function} emitFn — events.emit (for 'theme.changed')
 * @returns {object} theme
 */
export function createTheme(emitFn) {

    let _mqListener = null;
    const _listeners = new Set();  // local change subscribers

    function _apply(mode) {
        // Remove prior system-preference listener
        if (_mqListener) {
            window.matchMedia('(prefers-color-scheme: dark)').removeEventListener('change', _mqListener);
            _mqListener = null;
        }

        if (mode === 'system') {
            const resolved = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
            document.documentElement.setAttribute('data-theme', resolved);

            _mqListener = (e) => {
                if (_getMode() === 'system') {
                    const r = e.matches ? 'dark' : 'light';
                    document.documentElement.setAttribute('data-theme', r);
                    _notify(r);
                }
            };
            window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', _mqListener);
        } else {
            document.documentElement.setAttribute('data-theme', mode);
        }
    }

    function _getMode() {
        return localStorage.getItem(_key) || 'system';
    }

    function _getResolved() {
        const mode = _getMode();
        if (mode !== 'system') return mode;
        return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    }

    function _notify(resolved) {
        for (const fn of _listeners) {
            try { fn(resolved); } catch (e) { console.error('[sw.theme] Listener error:', e); }
        }
        emitFn('theme.changed', { theme: resolved }, { localOnly: true });
    }

    // ── Public API ──────────────────────────────

    return {
        get current() { return _getResolved(); },
        get mode()    { return _getMode(); },

        set(mode) {
            localStorage.setItem(_key, mode);
            _apply(mode);
            _notify(_getResolved());
        },

        /**
         * Subscribe to theme changes.
         * @param {string} event — 'change'
         * @param {Function} fn — receives resolved theme string
         * @returns {Function} unsubscribe
         */
        on(event, fn) {
            if (event !== 'change') {
                console.warn(`[sw.theme] Unknown event '${event}'`);
                return () => {};
            }
            _listeners.add(fn);
            return () => _listeners.delete(fn);
        },

        /** Apply stored theme. Called during boot. */
        init() {
            _apply(_getMode());
        },
    };
}
