"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get AppContextProvider () {
        return AppContextProvider;
    },
    get useAppContext () {
        return useAppContext;
    },
    get useConfig () {
        return useConfig;
    },
    get useLocale () {
        return useLocale;
    },
    get useStateManager () {
        return useStateManager;
    },
    get useTheme () {
        return useTheme;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _antd = require("antd");
const _library = require("../../library");
const _AppConfig = require("../config/AppConfig");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
/**
 * 主题上下文
 */ const ThemeContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
/**
 * 国际化上下文
 */ const LocaleContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
/**
 * 配置上下文
 */ const ConfigContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
/**
 * 状态管理器上下文
 */ const StateManagerContext = /*#__PURE__*/ (0, _react.createContext)(undefined);
/**
 * 主题 Provider 组件
 */ const ThemeProvider = /*#__PURE__*/ _react.default.memo(({ children, initialTheme })=>{
    const [theme, setThemeState] = (0, _react.useState)(initialTheme || _AppConfig.appConfig.getTheme());
    // 同步到单例
    (0, _react.useEffect)(()=>{
        _AppConfig.appConfig.setTheme(theme);
    }, [
        theme
    ]);
    // 更新主题，添加过渡动画支持
    const setTheme = (0, _react.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 = (0, _react.useMemo)(()=>({
            theme,
            setTheme
        }), [
        theme,
        setTheme
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(ThemeContext.Provider, {
        value: value,
        children: children
    });
});
ThemeProvider.displayName = 'ThemeProvider';
/**
 * 国际化 Provider 组件
 */ const LocaleProvider = /*#__PURE__*/ _react.default.memo(({ children, initialLocale })=>{
    const [locale, setLocaleState] = (0, _react.useState)(initialLocale || _AppConfig.appConfig.getLocale());
    const [isChanging, setIsChanging] = (0, _react.useState)(false);
    // 同步到单例
    (0, _react.useEffect)(()=>{
        _AppConfig.appConfig.setLocale(locale);
    }, [
        locale
    ]);
    // 更新语言，添加加载状态
    const setLocale = (0, _react.useCallback)((newLocale)=>{
        if (newLocale === locale) {
            return;
        }
        setIsChanging(true);
        setLocaleState(newLocale);
        (0, _library.setLang)(newLocale);
        // 使用 requestAnimationFrame 确保状态更新在下一帧
        requestAnimationFrame(()=>{
            setIsChanging(false);
        });
    }, [
        locale
    ]);
    const value = (0, _react.useMemo)(()=>({
            locale,
            setLocale,
            isChanging
        }), [
        locale,
        setLocale,
        isChanging
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(LocaleContext.Provider, {
        value: value,
        children: children
    });
});
LocaleProvider.displayName = 'LocaleProvider';
/**
 * 配置 Provider 组件
 */ const AppConfigProvider = /*#__PURE__*/ _react.default.memo(({ children, initialAntdConfig, initialInitializationContext })=>{
    const [antdConfig, setAntdConfigState] = (0, _react.useState)(initialAntdConfig || _AppConfig.appConfig.getAntdConfig());
    const [initializationContext, setInitializationContextState] = (0, _react.useState)(initialInitializationContext || _AppConfig.appConfig.getInitializationContext());
    // 同步到单例
    (0, _react.useEffect)(()=>{
        _AppConfig.appConfig.setAntdConfig(antdConfig);
    }, [
        antdConfig
    ]);
    (0, _react.useEffect)(()=>{
        _AppConfig.appConfig.setInitializationContext(initializationContext);
    }, [
        initializationContext
    ]);
    // 更新 Ant Design 配置
    const setAntdConfig = (0, _react.useCallback)((config)=>{
        setAntdConfigState(config);
    }, []);
    // 更新初始化上下文
    const setInitializationContext = (0, _react.useCallback)((context)=>{
        setInitializationContextState(context);
    }, []);
    const value = (0, _react.useMemo)(()=>({
            antdConfig,
            initializationContext,
            setAntdConfig,
            setInitializationContext
        }), [
        antdConfig,
        initializationContext,
        setAntdConfig,
        setInitializationContext
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.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.default.memo(({ children, stateManager })=>{
    const value = (0, _react.useMemo)(()=>({
            stateManager: stateManager ?? null
        }), [
        stateManager
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(StateManagerContext.Provider, {
        value: value,
        children: children
    });
});
StateManagerProvider.displayName = 'StateManagerProvider';
const AppContextProvider = ({ children, initialTheme, initialLocale, initialAntdConfig, initialInitializationContext, stateManager, antdApp })=>{
    // 初始化单例配置（仅执行一次）
    const initializedRef = _react.default.useRef(false);
    _react.default.useEffect(()=>{
        if (!initializedRef.current) {
            _AppConfig.appConfig.initialize({
                theme: initialTheme,
                locale: initialLocale,
                antdConfig: initialAntdConfig,
                initializationContext: initialInitializationContext
            });
            initializedRef.current = true;
        }
    }, [
        initialTheme,
        initialLocale,
        initialAntdConfig,
        initialInitializationContext
    ]);
    return /*#__PURE__*/ (0, _jsxruntime.jsx)(ThemeProvider, {
        initialTheme: initialTheme,
        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(LocaleProvider, {
            initialLocale: initialLocale,
            children: /*#__PURE__*/ (0, _jsxruntime.jsx)(AppConfigProvider, {
                initialAntdConfig: initialAntdConfig,
                initialInitializationContext: initialInitializationContext,
                children: /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.App, {
                    ...antdApp,
                    children: /*#__PURE__*/ (0, _jsxruntime.jsx)(StateManagerProvider, {
                        stateManager: stateManager,
                        children: children
                    })
                })
            })
        })
    });
};
const useTheme = ()=>{
    const context = (0, _react.useContext)(ThemeContext);
    if (context === undefined) {
        throw new Error('useTheme 必须在 AppContextProvider 内使用');
    }
    return context;
};
const useLocale = ()=>{
    const context = (0, _react.useContext)(LocaleContext);
    if (context === undefined) {
        throw new Error('useLocale 必须在 AppContextProvider 内使用');
    }
    return context;
};
const useConfig = ()=>{
    const context = (0, _react.useContext)(ConfigContext);
    if (context === undefined) {
        throw new Error('useConfig 必须在 AppContextProvider 内使用');
    }
    return context;
};
const useStateManager = ()=>{
    const context = (0, _react.useContext)(StateManagerContext);
    if (context === undefined) {
        throw new Error('useStateManager 必须在 AppContextProvider 内使用');
    }
    return context.stateManager;
};
const useAppContext = ()=>{
    const themeContext = useTheme();
    const localeContext = useLocale();
    const configContext = useConfig();
    const stateManagerContext = useStateManager();
    // 优化：使用更细粒度的依赖项，避免不必要的重新计算
    // 只依赖实际变化的值，函数引用是稳定的
    return (0, _react.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