import { jsx as _jsx } from "react/jsx-runtime";
import React from 'react';
import { defaultTheme } from './defaultTheme';
import { darkTheme } from './darkTheme';
import { createTheme } from './utils';
const ThemeContext = React.createContext({
    theme: defaultTheme,
    themeName: defaultTheme.name,
    setThemeName: () => undefined,
    toggleTheme: () => undefined,
    canToggle: false
});
export const ThemeProvider = ({ children, theme, overrides, initialThemeName }) => {
    const isControlled = theme !== undefined;
    const [internalThemeName, setInternalThemeName] = React.useState(() => {
        if (theme) {
            return theme.name === 'dark' ? 'dark' : 'light';
        }
        if (initialThemeName) {
            return initialThemeName;
        }
        if (typeof window !== 'undefined' && typeof window.matchMedia === 'function') {
            return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
        }
        return 'light';
    });
    React.useEffect(() => {
        if (isControlled && theme) {
            setInternalThemeName(theme.name === 'dark' ? 'dark' : 'light');
        }
    }, [isControlled, theme]);
    React.useEffect(() => {
        if (!isControlled && initialThemeName) {
            setInternalThemeName(initialThemeName);
        }
    }, [isControlled, initialThemeName]);
    const lightTheme = React.useMemo(() => createTheme(overrides), [overrides]);
    const darkThemeWithOverrides = React.useMemo(() => createTheme(overrides, darkTheme), [overrides]);
    const resolvedTheme = React.useMemo(() => {
        if (isControlled && theme) {
            return theme;
        }
        return internalThemeName === 'dark' ? darkThemeWithOverrides : lightTheme;
    }, [isControlled, theme, internalThemeName, darkThemeWithOverrides, lightTheme]);
    const currentThemeName = React.useMemo(() => {
        if (isControlled) {
            if (theme && theme.name === 'dark') {
                return 'dark';
            }
            return 'light';
        }
        return internalThemeName;
    }, [isControlled, theme, internalThemeName]);
    const setThemeName = React.useCallback((name) => {
        if (isControlled) {
            return;
        }
        setInternalThemeName(name === 'dark' ? 'dark' : 'light');
    }, [isControlled]);
    const toggleTheme = React.useCallback(() => {
        if (isControlled) {
            return;
        }
        setInternalThemeName((prev) => (prev === 'dark' ? 'light' : 'dark'));
    }, [isControlled]);
    const value = React.useMemo(() => ({
        theme: resolvedTheme,
        themeName: currentThemeName,
        setThemeName,
        toggleTheme,
        canToggle: !isControlled
    }), [resolvedTheme, currentThemeName, setThemeName, toggleTheme, isControlled]);
    return (_jsx(ThemeContext.Provider, { value: value, children: children }));
};
export const useTheme = () => {
    const { theme } = React.useContext(ThemeContext);
    return theme;
};
export const useThemeName = () => {
    const { themeName } = React.useContext(ThemeContext);
    return themeName;
};
export const useThemeControls = () => {
    const { themeName, setThemeName, toggleTheme, canToggle } = React.useContext(ThemeContext);
    return {
        themeName,
        setThemeName,
        toggleTheme,
        canToggle
    };
};
