import { jsx } from "react/jsx-runtime";
import { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
import { ConfigProvider, theme as external_antd_theme } from "antd";
import lodash from "lodash";
import defaultTheme from "./defaultTheme.mjs";
import AntdAppProvider from "./AntdAppProvider.mjs";
export * from "./type.mjs";
const GlobalThemeContext = /*#__PURE__*/ createContext(null);
GlobalThemeContext.displayName = 'GlobalThemeContext';
const useGlobalTheme = ()=>useContext(GlobalThemeContext) || {
        theme: {},
        isDarkTheme: false
    };
const GlobalThemeProvider = (param)=>{
    let { children, theme: themeFromProps } = param;
    const [theme, setTheme] = useState(themeFromProps || defaultTheme);
    const currentSettingThemeRef = useRef(null);
    const currentEditingThemeRef = useRef(null);
    const isDarkTheme = useMemo(()=>{
        const algorithm = null == theme ? void 0 : theme.algorithm;
        if (Array.isArray(algorithm)) return algorithm.includes(external_antd_theme.darkAlgorithm);
        return algorithm === external_antd_theme.darkAlgorithm;
    }, [
        null == theme ? void 0 : theme.algorithm
    ]);
    const setCurrentEditingTheme = useCallback((themeItem)=>{
        currentEditingThemeRef.current = themeItem ? lodash.cloneDeep(themeItem) : themeItem;
    }, []);
    const getCurrentEditingTheme = useCallback(()=>currentEditingThemeRef.current, []);
    const setCurrentSettingTheme = useCallback((theme)=>{
        currentSettingThemeRef.current = theme ? lodash.cloneDeep(theme) : theme;
    }, []);
    const getCurrentSettingTheme = useCallback(()=>currentSettingThemeRef.current, []);
    const value = useMemo(()=>({
            theme,
            setTheme,
            setCurrentSettingTheme,
            getCurrentSettingTheme,
            setCurrentEditingTheme,
            getCurrentEditingTheme,
            isDarkTheme
        }), [
        getCurrentEditingTheme,
        getCurrentSettingTheme,
        isDarkTheme,
        setCurrentEditingTheme,
        setCurrentSettingTheme,
        theme
    ]);
    return /*#__PURE__*/ jsx(GlobalThemeContext.Provider, {
        value: value,
        children: /*#__PURE__*/ jsx(ConfigProvider, {
            theme: defaultTheme,
            children: /*#__PURE__*/ jsx(ConfigProvider, {
                theme: theme,
                children: children
            })
        })
    });
};
export { AntdAppProvider, GlobalThemeProvider, defaultTheme, useGlobalTheme };
