// 引入Provider
import { Provider } from 'react-redux';
// 引入仓库
import store from './store';
import Head from './layout/head/Head.tsx';
import Body from './layout/body/Body.tsx';
// import Foot from './layout/foot/Foot.tsx';
import {
    GlobalContext,
    GlobalData,
    GlobalDataAction,
    GlobalDataActionType,
    GlobalDispatchContext,
} from "./context/GlobalContext.ts";
import React, { Reducer, useReducer } from "react";

const App: React.FC = () => {

    const globalDataReducer = (state: GlobalData, action: GlobalDataAction) => {
        if (action.type == GlobalDataActionType.REFRESH_SELF) {
            if (action.user) {
                state.user.balance = action.user.balance;
                state.user.userId = action.user.userId;
                state.user.nickname = action.user.nickname;
                state.user.username = action.user.username;
                state.user.hasUserInfo = action.user.hasUserInfo;
            }
        }
        return {
            ...state
        }
    }
    const [globalData, dispatch] = useReducer<Reducer<GlobalData, GlobalDataAction>>(globalDataReducer, {
        user: {
            balance: '0',
            userId: '',
            username: '',
            nickname: '',
            hasUserInfo: false
        }
    });

    return (
        <>
            <Provider store={store}>
                <GlobalContext.Provider value={globalData}>
                    <GlobalDispatchContext.Provider value={dispatch}>
                        <Head />
                        <Body />
                        {/* <Foot/> */}
                    </GlobalDispatchContext.Provider>
                </GlobalContext.Provider>
            </Provider>
        </>
    );
};

export default App;