import type { Dict } from './../../api/interface/index'
import { defineStore } from 'pinia'
import piniaPersistConfig from '@/stores/helper/persist'
import { DICT_STORE_KEY } from '@/stores/constant'
// 模拟数据
//import dictData from '@/api/system/dictData'


export const useDictStore = defineStore({
    id: DICT_STORE_KEY,
    state: () => ({
        dictData: [] as Dict[],
    }),
    getters: {},
    actions: {
        setDictData(data: Dict[]) {
            this.dictData = data
        },
        // 获取字典数据数组，如果为空则返回空数组
        getDictData(code: string) {
            if (!this.dictData.length) {
                return []
            }
            const dict = this.dictData.find(item => item.code === code)
            return dict?.children || []
        },
        // 初始化获取全部字典数据并缓存
        async initDictData(initData: Dict[]) {
            this.dictData = initData
        },
    },
    persist: piniaPersistConfig(DICT_STORE_KEY),
})
