import { create } from 'zustand';
import { apis } from '@/apis';

export interface AppTypeItem {
    label: string;
    value: string;
    remark?: string;
}

interface AppState {
    standardClass: AppTypeItem[];
}

interface AppActions {
    setStandardClass: (data: AppTypeItem[]) => void;
    fetchAppstandardClass: () => Promise<void>;
    getAppState: () => Promise<AppTypeItem[]>;
}

export type AppStore = AppState & AppActions;

export const useAppStore = create<AppStore>((set, get) => ({
    standardClass: [],
    
    setStandardClass: (data) => set({ standardClass: data }),
    
    fetchAppstandardClass: async () => {
        try {
            const res = await apis.fetchTakaiAppTypeList('standard_classification');
            const list = res.data.map((item: any) => ({
                label: item.dictLabel,
                value: item.dictValue,
                remark: item.remark,
            }));
            set({ standardClass: list });
        } catch (error: any) {
            console.error('获取分类字典失败:', error);
        }
    },
    
    getAppState: async () => {
        const { standardClass, fetchAppstandardClass } = get();
        if (standardClass.length === 0) {
            await fetchAppstandardClass();
        }
        return get().standardClass;
    },
}));

export default useAppStore;
