import useDictStore from '@/store_v3/modules/dict'
import { getDicts } from '@/api/system/dict/data'
import { ref, toRefs } from 'vue'

/**
 * 获取字典数据
 */
export function useDict(...args) {
  let res = {};
  return (() => {
    args.forEach((dictType, index) => {
      res[dictType] = [];
      const dicts = useDictStore().getDict(dictType);
      if (dicts) {
        res[dictType] = dicts;
      } else {
        getDicts(dictType).then(resp => {
          res[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue }))
          useDictStore().setDict(dictType, res[dictType]);
        })
      }
    })
    const res2 = ref(res);
    return toRefs(res2.value);
  })()
}