"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThemeProvider = exports.useTheme = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * ThemeProvider Component
 *
 * Provides theme context and functionality for the layout system
 */
const react_1 = require("react");
const prop_types_1 = __importDefault(require("prop-types"));
const useThemeManager_js_1 = require("../hooks/useThemeManager.js");
// Create Theme Context
const ThemeContext = (0, react_1.createContext)({
    theme: 'light',
    setTheme: () => { },
    toggleTheme: () => { },
    isTheme: () => false,
    isDark: false,
    isLight: true
});
/**
 * Hook to use the theme context
 * @returns {Object} Theme context value
 */
const useTheme = () => (0, react_1.useContext)(ThemeContext);
exports.useTheme = useTheme;
/**
 * ThemeProvider component
 * @param {Object} props - Component props
 * @param {React.ReactNode} props.children - Child components
 * @param {string} props.defaultTheme - Default theme (default: 'light')
 * @param {boolean} props.useSystemPreference - Whether to use system color scheme preference (default: true)
 * @param {Object} props.themes - Custom theme definitions
 * @returns {React.ReactElement} ThemeProvider component
 */
const ThemeProvider = ({ children, defaultTheme = 'light', useSystemPreference = true, themes = {}, ...rest }) => {
    // Use theme manager hook
    const themeManager = (0, useThemeManager_js_1.useThemeManager)(defaultTheme);
    // Handle system preference
    (0, react_1.useEffect)(() => {
        if (useSystemPreference) {
            const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
            const handleChange = (e) => {
                themeManager.setTheme(e.matches ? 'dark' : 'light');
            };
            // Set initial value
            if (mediaQuery.matches) {
                themeManager.setTheme('dark');
            }
            // Add listener
            mediaQuery.addEventListener('change', handleChange);
            // Clean up
            return () => mediaQuery.removeEventListener('change', handleChange);
        }
    }, [useSystemPreference, themeManager]);
    // Apply custom theme CSS variables
    (0, react_1.useEffect)(() => {
        if (themes && Object.keys(themes).length > 0) {
            const currentTheme = themes[themeManager.theme] || {};
            // Apply CSS variables
            Object.entries(currentTheme).forEach(([property, value]) => {
                document.documentElement.style.setProperty(`--theme-${property}`, value);
            });
        }
    }, [themeManager.theme, themes]);
    return ((0, jsx_runtime_1.jsx)(ThemeContext.Provider, { value: themeManager, ...rest, children: children }));
};
exports.ThemeProvider = ThemeProvider;
exports.ThemeProvider.propTypes = {
    children: prop_types_1.default.node.isRequired,
    defaultTheme: prop_types_1.default.string,
    useSystemPreference: prop_types_1.default.bool,
    themes: prop_types_1.default.object
};
exports.default = exports.ThemeProvider;
//# sourceMappingURL=ThemeProvider.js.map