import { makeAutoObservable } from 'mobx';
import { lazy, Suspense, useEffect } from 'react';
import { apis } from '@/apis';

type AppTypeList = {
    label: string,
    value: string,
    children: {
        label: string,
        value: string,
    }[],
}[];
interface State {
    standardClass: AppTypeList[];
}
const appState = (): State => ({
    standardClass: [],
})

const setUseState = (state: State) => {
    return {
        setState: (data: AppTypeList[]) => {
            state.standardClass = data;
        }
    }
}


const state = makeAutoObservable(appState());
const actions = setUseState(state);
const useAppState = () => {
    // 获取分类字典
    const fetchAppstandardClass = async () => {
        try {
            const res = await apis.fetchTakaiAppTypeList('standard_classification');
            const list = res.data.map((item: any) => {
                return {
                    label: item.dictLabel,
                    value: item.dictValue,
                    remark: item.remark,
                }
            });
            actions.setState(list);
        } catch (error: any) {
            console.error(error);
        }
    }
    // 获取
    const getAppState = async () => {
        if (state.standardClass.length === 0) {
            await fetchAppstandardClass();
            return state.standardClass
        }
        return state.standardClass
    }

    return {
        appState: state,
        getAppState
    }
}

export default useAppState;