import { defineStore } from 'pinia'
import { DICT_DATA } from '@/stores/constant/cacheKey'
import type { DictData, BasicDictData } from '@/stores/interface/index'
import { reactive } from 'vue'
import { ITEM_RENDER_EVT } from 'element-plus/es/components/virtual-list/src/defaults'
import { log } from 'console'

export const useDictData = defineStore(
    'dictData',
    () => {
        const state: DictData = reactive({
            basic: [],
            area: [],
            areaTree: []
            // 其他接口获取的字典，比如区域
        })
        const getBasicData = (code: string, arr?: string[]) => {
            let list = []
            list = state.basic.filter(item => item.code === code)[0]?.children || []
            if (arr) {
                list = list.filter(item => !arr.includes(item.code))
            }
            return list
        }
        const areaSelect = () => {
            let list = state.areaTree.filter(item => item.id == state.area[0]?.area)

            return list.length == 0 ? state.areaTree : list
        }

        return {
            state,
            getBasicData,
            areaSelect
        }
    },
    {
        persist: {
            key: DICT_DATA
        }
    }
)
