import { jsx as _jsx } from "react/jsx-runtime";
import React, { createContext, useContext, useState, useCallback, useMemo, useEffect } from "react";
import { App } from "antd";
import { setLang } from "../../library";
import { appConfig } from "../config/AppConfig";
/**
 * 主题上下文
 */ const ThemeContext = /*#__PURE__*/ createContext(undefined);
/**
 * 国际化上下文
 */ const LocaleContext = /*#__PURE__*/ createContext(undefined);
/**
 * 配置上下文
 */ const ConfigContext = /*#__PURE__*/ createContext(undefined);
/**
 * 状态管理器上下文
 */ const StateManagerContext = /*#__PURE__*/ createContext(undefined);
/**
 * 主题 Provider 组件
 */ const ThemeProvider = /*#__PURE__*/ React.memo(({ children, initialTheme })=>{
    const [theme, setThemeState] = useState(initialTheme || appConfig.getTheme());
    // 同步到单例
    useEffect(()=>{
        appConfig.setTheme(theme);
    }, [
        theme
    ]);
    // 更新主题，添加过渡动画支持
    const setTheme = useCallback((newTheme)=>{
        setThemeState((prev)=>{
            const updated = typeof newTheme === 'function' ? newTheme(prev) : newTheme;
            // 如果主题模式改变，更新文档类名（添加过渡效果）
            if (updated.mode !== prev.mode) {
                // 使用 requestAnimationFrame 确保 DOM 更新在下一帧
                requestAnimationFrame(()=>{
                    const html = document.documentElement;
                    html.classList.remove('light', 'dark');
                    // 添加过渡类（如果支持）
                    if (html.style.transition === undefined) {
                        html.style.transition = 'background-color 0.3s ease, color 0.3s ease';
                    }
                    if (updated.mode && updated.mode !== 'auto') {
                        html.classList.add(updated.mode);
                    } else if (updated.mode === 'auto') {
                        // 跟随系统设置
                        const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
                        html.classList.add(prefersDark ? 'dark' : 'light');
                    }
                });
            }
            return updated;
        });
    }, []);
    const value = useMemo(()=>({
            theme,
            setTheme
        }), [
        theme,
        setTheme
    ]);
    return /*#__PURE__*/ _jsx(ThemeContext.Provider, {
        value: value,
        children: children
    });
});
ThemeProvider.displayName = 'ThemeProvider';
/**
 * 国际化 Provider 组件
 */ const LocaleProvider = /*#__PURE__*/ React.memo(({ children, initialLocale })=>{
    const [locale, setLocaleState] = useState(initialLocale || appConfig.getLocale());
    const [isChanging, setIsChanging] = useState(false);
    // 同步到单例
    useEffect(()=>{
        appConfig.setLocale(locale);
    }, [
        locale
    ]);
    // 更新语言，添加加载状态
    const setLocale = useCallback((newLocale)=>{
        if (newLocale === locale) {
            return;
        }
        setIsChanging(true);
        setLocaleState(newLocale);
        setLang(newLocale);
        // 使用 requestAnimationFrame 确保状态更新在下一帧
        requestAnimationFrame(()=>{
            setIsChanging(false);
        });
    }, [
        locale
    ]);
    const value = useMemo(()=>({
            locale,
            setLocale,
            isChanging
        }), [
        locale,
        setLocale,
        isChanging
    ]);
    return /*#__PURE__*/ _jsx(LocaleContext.Provider, {
        value: value,
        children: children
    });
});
LocaleProvider.displayName = 'LocaleProvider';
/**
 * 配置 Provider 组件
 */ const AppConfigProvider = /*#__PURE__*/ React.memo(({ children, initialAntdConfig, initialInitializationContext })=>{
    const [antdConfig, setAntdConfigState] = useState(initialAntdConfig || appConfig.getAntdConfig());
    const [initializationContext, setInitializationContextState] = useState(initialInitializationContext || appConfig.getInitializationContext());
    // 同步到单例
    useEffect(()=>{
        appConfig.setAntdConfig(antdConfig);
    }, [
        antdConfig
    ]);
    useEffect(()=>{
        appConfig.setInitializationContext(initializationContext);
    }, [
        initializationContext
    ]);
    // 更新 Ant Design 配置
    const setAntdConfig = useCallback((config)=>{
        setAntdConfigState(config);
    }, []);
    // 更新初始化上下文
    const setInitializationContext = useCallback((context)=>{
        setInitializationContextState(context);
    }, []);
    const value = useMemo(()=>({
            antdConfig,
            initializationContext,
            setAntdConfig,
            setInitializationContext
        }), [
        antdConfig,
        initializationContext,
        setAntdConfig,
        setInitializationContext
    ]);
    return /*#__PURE__*/ _jsx(ConfigContext.Provider, {
        value: value,
        children: children
    });
});
AppConfigProvider.displayName = 'AppConfigProvider';
/**
 * 应用上下文 Provider 组件
 * 
 * 组合所有 Provider，提供统一的全局上下文
 * 
 * @example
 * ```tsx
 * <AppContextProvider
 *   initialTheme={{ mode: 'light', primaryColor: '#1890ff' }}
 *   initialLocale="zh-CN"
 * >
 *   <App />
 * </AppContextProvider>
 * ```
 */ /**
 * 状态管理器 Provider 组件
 */ const StateManagerProvider = /*#__PURE__*/ React.memo(({ children, stateManager })=>{
    const value = useMemo(()=>({
            stateManager: stateManager ?? null
        }), [
        stateManager
    ]);
    return /*#__PURE__*/ _jsx(StateManagerContext.Provider, {
        value: value,
        children: children
    });
});
StateManagerProvider.displayName = 'StateManagerProvider';
export const AppContextProvider = ({ children, initialTheme, initialLocale, initialAntdConfig, initialInitializationContext, stateManager, antdApp })=>{
    // 初始化单例配置（仅执行一次）
    const initializedRef = React.useRef(false);
    React.useEffect(()=>{
        if (!initializedRef.current) {
            appConfig.initialize({
                theme: initialTheme,
                locale: initialLocale,
                antdConfig: initialAntdConfig,
                initializationContext: initialInitializationContext
            });
            initializedRef.current = true;
        }
    }, [
        initialTheme,
        initialLocale,
        initialAntdConfig,
        initialInitializationContext
    ]);
    return /*#__PURE__*/ _jsx(ThemeProvider, {
        initialTheme: initialTheme,
        children: /*#__PURE__*/ _jsx(LocaleProvider, {
            initialLocale: initialLocale,
            children: /*#__PURE__*/ _jsx(AppConfigProvider, {
                initialAntdConfig: initialAntdConfig,
                initialInitializationContext: initialInitializationContext,
                children: /*#__PURE__*/ _jsx(App, {
                    ...antdApp,
                    children: /*#__PURE__*/ _jsx(StateManagerProvider, {
                        stateManager: stateManager,
                        children: children
                    })
                })
            })
        })
    });
};
/**
 * 使用主题上下文 Hook
 * 
 * @returns 主题上下文值
 * @throws 如果不在 ThemeProvider 内使用会抛出错误
 * 
 * @example
 * ```tsx
 * const { theme, setTheme } = useTheme();
 * 
 * // 切换主题
 * setTheme({ mode: 'dark' });
 * ```
 */ export const useTheme = ()=>{
    const context = useContext(ThemeContext);
    if (context === undefined) {
        throw new Error('useTheme 必须在 AppContextProvider 内使用');
    }
    return context;
};
/**
 * 使用国际化上下文 Hook
 * 
 * @returns 国际化上下文值
 * @throws 如果不在 LocaleProvider 内使用会抛出错误
 * 
 * @example
 * ```tsx
 * const { locale, setLocale } = useLocale();
 * 
 * // 切换语言
 * setLocale('en-US');
 * ```
 */ export const useLocale = ()=>{
    const context = useContext(LocaleContext);
    if (context === undefined) {
        throw new Error('useLocale 必须在 AppContextProvider 内使用');
    }
    return context;
};
/**
 * 使用配置上下文 Hook
 * 
 * @returns 配置上下文值
 * @throws 如果不在 ConfigProvider 内使用会抛出错误
 */ export const useConfig = ()=>{
    const context = useContext(ConfigContext);
    if (context === undefined) {
        throw new Error('useConfig 必须在 AppContextProvider 内使用');
    }
    return context;
};
/**
 * 使用应用上下文 Hook（聚合所有上下文）
 * 
 * @returns 应用上下文值
 * @throws 如果不在 AppContextProvider 内使用会抛出错误
 * 
 * @example
 * ```tsx
 * const { theme, setTheme, locale, setLocale, antdConfig } = useAppContext();
 * 
 * // 切换主题
 * setTheme({ mode: 'dark' });
 * 
 * // 切换语言
 * setLocale('en-US');
 * ```
 */ /**
 * 使用状态管理器 Hook
 * 
 * @returns 状态管理器实例
 * @throws 如果不在 AppContextProvider 内使用会抛出错误
 * 
 * @example
 * ```tsx
 * const stateManager = useStateManager();
 * 
 * // 创建状态实例
 * const countState = stateManager?.createState(0);
 * ```
 */ export const useStateManager = ()=>{
    const context = useContext(StateManagerContext);
    if (context === undefined) {
        throw new Error('useStateManager 必须在 AppContextProvider 内使用');
    }
    return context.stateManager;
};
export const useAppContext = ()=>{
    const themeContext = useTheme();
    const localeContext = useLocale();
    const configContext = useConfig();
    const stateManagerContext = useStateManager();
    // 优化：使用更细粒度的依赖项，避免不必要的重新计算
    // 只依赖实际变化的值，函数引用是稳定的
    return useMemo(()=>({
            theme: themeContext.theme,
            locale: localeContext.locale,
            antdConfig: configContext.antdConfig,
            initializationContext: configContext.initializationContext,
            stateManager: stateManagerContext,
            setTheme: themeContext.setTheme,
            setLocale: localeContext.setLocale,
            setAntdConfig: configContext.setAntdConfig,
            setInitializationContext: configContext.setInitializationContext
        }), [
        themeContext.theme,
        localeContext.locale,
        configContext.antdConfig,
        configContext.initializationContext,
        stateManagerContext,
        // 函数引用是稳定的，不需要作为依赖
        themeContext.setTheme,
        localeContext.setLocale,
        configContext.setAntdConfig,
        configContext.setInitializationContext
    ]);
};

//# sourceMappingURL=AppContext.js.map