import { useDictStore } from '@/stores/modules/dict';
import { ref, toRefs } from 'vue';
import { getDictByCode } from '@/api/menu';

const dictStore = useDictStore();
const res = ref({});
export const useDict = (...args) => {
  return (() => {
    args.forEach(async (dictType, index) => {
      res.value[dictType] = [];
      const dicts = await dictStore.getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        getDictByCode(dictType)
          .then((resp) => {
            if (resp && resp.result && resp.result.length) {
              res.value[dictType] = resp.result.map((d) => ({
                label: d.desc,
                value: d.value,
              }));
              dictStore.setDict({ key: dictType, value: res.value[dictType] });
            }
          })
          .catch((e) => {
            console.log('eee', e);
          });
      }
    });
    return toRefs(res.value);
  })();
};
