import { defineStore } from 'pinia'
import { store } from '/@/store'
import * as UserInfoAPI from '@/api/model/userInfo'

interface dictItem {
    color: string
    jsonObject?: string
    label: string
    text: string
    title: string
    value: string | number
}
// 定义字典列表的类型
export interface DictListVO {
    [key: string]: dictItem[]
}
export const useDictStore = defineStore({
    id: 'dict',
    state: () => ({
        dictList: {} as DictListVO,
    }),
    getters: {
        getDictList(): DictListVO {
            if (Object.keys(this.dictList).length === 0) {
                UserInfoAPI.dictList().then((res) => {
                    this.dictList = res
                })
                return this.dictList
            }
            return this.dictList
        },
    },
    actions: {
        setDictList(dictList: DictListVO): void {
            this.dictList = dictList
        },
        /**
         * 清空字典数据
         */
        clearDict(): void {
            this.dictList = {}
        },
    },
})

// Need to be used outside the setup
export function useAppStoreWithOut() {
    return useDictStore(store)
}
