/**
 * Theme Context for Code Buddy
 * Provides theme and avatar information to all UI components
 */
import React, { createContext, useContext, useState, useCallback, useMemo } from 'react';
import { getThemeManager } from '../../themes/theme-manager.js';
/**
 * Create the theme context
 */
const ThemeContext = createContext(null);
/**
 * Theme provider component
 */
export function ThemeProvider({ children }) {
    const [themeManager] = useState(() => getThemeManager());
    const [refreshKey, setRefreshKey] = useState(0);
    // Force re-render when theme changes
    const refreshTheme = useCallback(() => {
        setRefreshKey(prev => prev + 1);
    }, []);
    // Get current theme data
    const theme = themeManager.getCurrentTheme();
    const colors = themeManager.getColors();
    const avatars = themeManager.getAvatars();
    // Theme management functions
    const setTheme = useCallback((themeId) => {
        const result = themeManager.setTheme(themeId);
        if (result) {
            refreshTheme();
        }
        return result;
    }, [themeManager, refreshTheme]);
    const getAvailableThemes = useCallback(() => {
        return themeManager.getAvailableThemes();
    }, [themeManager]);
    // Avatar management functions
    const setAvatarPreset = useCallback((presetId) => {
        const result = themeManager.applyAvatarPreset(presetId);
        if (result) {
            refreshTheme();
        }
        return result;
    }, [themeManager, refreshTheme]);
    const setCustomAvatar = useCallback((key, value) => {
        themeManager.setCustomAvatar(key, value);
        refreshTheme();
    }, [themeManager, refreshTheme]);
    const clearCustomAvatars = useCallback(() => {
        themeManager.clearCustomAvatars();
        refreshTheme();
    }, [themeManager, refreshTheme]);
    const getAvatarPresets = useCallback(() => {
        return themeManager.getAvatarPresets();
    }, [themeManager]);
    // Color management functions
    const setCustomColor = useCallback((key, value) => {
        themeManager.setCustomColor(key, value);
        refreshTheme();
    }, [themeManager, refreshTheme]);
    const clearCustomColors = useCallback(() => {
        themeManager.clearCustomColors();
        refreshTheme();
    }, [themeManager, refreshTheme]);
    // Memoize context value to prevent unnecessary re-renders of consumers
    // refreshKey is included to trigger updates when theme changes
    const contextValue = useMemo(() => ({
        theme,
        colors,
        avatars,
        setTheme,
        getAvailableThemes,
        setAvatarPreset,
        setCustomAvatar,
        clearCustomAvatars,
        getAvatarPresets,
        setCustomColor,
        clearCustomColors,
        refreshTheme,
    }), [
        theme, colors, avatars, refreshKey,
        setTheme, getAvailableThemes, setAvatarPreset,
        setCustomAvatar, clearCustomAvatars, getAvatarPresets,
        setCustomColor, clearCustomColors, refreshTheme
    ]);
    // Note: Removed key={refreshKey} antipattern - it caused entire tree to unmount/remount
    // The useMemo with refreshKey dependency properly triggers re-renders without destroying state
    return (React.createElement(ThemeContext.Provider, { value: contextValue }, children));
}
/**
 * Hook to access theme context
 */
export function useTheme() {
    const context = useContext(ThemeContext);
    if (!context) {
        throw new Error('useTheme must be used within a ThemeProvider');
    }
    return context;
}
/**
 * Hook to get only colors (for components that only need colors)
 */
export function useThemeColors() {
    const { colors } = useTheme();
    return colors;
}
/**
 * Hook to get only avatars (for components that only need avatars)
 */
export function useAvatars() {
    const { avatars } = useTheme();
    return avatars;
}
//# sourceMappingURL=theme-context.js.map