import * as dictsApi from '@/api/dicts';

export default {
  namespaced: true,
  state: {
    dicts: {},
    caches: {},
  },
  mutations: {

  },
  actions: {
    getDict({ state, commit }, type) {
      const fetchDict = (name) => {
        if (state.dicts[name]?.length || state.dicts[`${name}Loading`]) return Promise.resolve();
        commit('SET_CONSTANT', { key: `${name}Loading`, val: true });
        return dictsApi[name]?.().then((res) => {
          commit('SET_CONSTANT', { key: name, val: res });
          commit('SET_CONSTANT', { key: `${name}Loading`, val: false });
        });
      };
      if (Array.isArray(type)) {
        return Promise.all(type.map(fetchDict));
      }
      return fetchDict(type);
    },
  },
};
