/**
 * vuex getter
 */
import dictEnum from '@/enum/dict';
import store from './index';

export default {
  user: (state) => state.user,
  theme: (state) => state.theme,
  dict: (state) => state.dict,
  taskObj: (state) => state.taskObj,
  // 根据字典enumName  和 dictCode 获取字典项
  getDict: (state) => (enumName, dictCode) =>
    (state.dict[dictEnum[enumName]] || []).find(
      (item) => item.dictCode === dictCode
    ) || {},
  // 根据字典enumName  和 dictCode 获取字典 值（名称
  getDictValue: (state) => (enumName, dictCode) => {
    const obj = (state.dict[dictEnum[enumName]] || []).find((item) => {
      return item.dictCode === dictCode + '';
    });

    if (!state.dict[dictEnum[enumName]]) {
      // 提交dict模块下的actions的requestDict
      store.dispatch('dict/requestDict', enumName);
      return '';
    }

    return obj && obj.dictValue;
  },
  // 根据字典enumName 获取字典列表 label value 格式
  getDictListByName: (state) => (enumName) => {
    const dList = state.dict[dictEnum[enumName]] || [];

    if (!state.dict[dictEnum[enumName]]) {
      // 提交dict模块下的actions的requestDict
      store.dispatch('dict/requestDict', enumName);
      return '';
    }

    return dList.map((i) => {
      return {
        ...i,
        label: i.dictValue,
        value: i.dictCode
      };
    });
  }
};
