import { useCallback, useMemo, useSyncExternalStore } from 'react';
const STORAGE_KEY = 'agi-preferences';
const DEFAULT_PREFERENCES = {
    vimMode: false,
    reasoningEnabled: true,
};
function resolveInitialPreferences() {
    if (typeof window === 'undefined') {
        return DEFAULT_PREFERENCES;
    }
    try {
        const stored = window.localStorage.getItem(STORAGE_KEY);
        if (stored) {
            const parsed = JSON.parse(stored);
            return {
                ...DEFAULT_PREFERENCES,
                ...parsed,
            };
        }
    }
    catch (error) {
        console.warn('Failed to load preferences', error);
    }
    return DEFAULT_PREFERENCES;
}
let preferences = resolveInitialPreferences();
const listeners = new Set();
function getSnapshot() {
    return preferences;
}
function getServerSnapshot() {
    return DEFAULT_PREFERENCES;
}
function subscribe(listener) {
    listeners.add(listener);
    return () => listeners.delete(listener);
}
function notifyListeners() {
    for (const listener of listeners) {
        listener();
    }
}
function updateStore(updates) {
    preferences = { ...preferences, ...updates };
    if (typeof window !== 'undefined') {
        try {
            window.localStorage.setItem(STORAGE_KEY, JSON.stringify(preferences));
        }
        catch (error) {
            console.warn('Failed to persist preferences', error);
        }
    }
    notifyListeners();
}
export function usePreferences() {
    const currentPreferences = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
    const updatePreferences = useCallback((updates) => {
        updateStore(updates);
    }, []);
    return useMemo(() => ({ preferences: currentPreferences, updatePreferences }), [currentPreferences, updatePreferences]);
}
//# sourceMappingURL=usePreferences.js.map