import Vue from 'vue'
import store from '@/store'
import DataDict from '@/utils/dict'
import {getDicts as getDicts} from '@/api/system/dict/data'
let dictDataMap =
  {
    // 系统开关
    "sys_normal_disable": [
      {
        "createBy": "admin",
        "createTime": "2023-04-23 16:13:26",
        "updateBy": null,
        "updateTime": null,
        "remark": "正常状态",
        "dictCode": 6,
        "dictSort": 1,
        "dictLabel": "正常",
        "dictValue": "0",
        "dictType": "sys_normal_disable",
        "cssClass": "",
        "listClass": "primary",
        "isDefault": "Y",
        "status": "0",
        "default": true
      },
      {
        "createBy": "admin",
        "createTime": "2023-04-23 16:13:26",
        "updateBy": null,
        "updateTime": null,
        "remark": "停用状态",
        "dictCode": 7,
        "dictSort": 2,
        "dictLabel": "停用",
        "dictValue": "1",
        "dictType": "sys_normal_disable",
        "cssClass": "",
        "listClass": "danger",
        "isDefault": "N",
        "status": "0",
        "default": false
      }
    ],
    // 用户性别
    "sys_user_sex": [
      {
        "createBy": "admin",
        "createTime": "2023-04-23 16:13:24",
        "updateBy": null,
        "updateTime": null,
        "remark": "性别男",
        "dictCode": 1,
        "dictSort": 1,
        "dictLabel": "男",
        "dictValue": "0",
        "dictType": "sys_user_sex",
        "cssClass": "",
        "listClass": "",
        "isDefault": "Y",
        "status": "0",
        "default": true
      },
      {
        "createBy": "admin",
        "createTime": "2023-04-23 16:13:24",
        "updateBy": null,
        "updateTime": null,
        "remark": "性别女",
        "dictCode": 2,
        "dictSort": 2,
        "dictLabel": "女",
        "dictValue": "1",
        "dictType": "sys_user_sex",
        "cssClass": "",
        "listClass": "",
        "isDefault": "N",
        "status": "0",
        "default": false
      },
      {
        "createBy": "admin",
        "createTime": "2023-04-23 16:13:25",
        "updateBy": null,
        "updateTime": null,
        "remark": "性别未知",
        "dictCode": 3,
        "dictSort": 3,
        "dictLabel": "未知",
        "dictValue": "2",
        "dictType": "sys_user_sex",
        "cssClass": "",
        "listClass": "",
        "isDefault": "N",
        "status": "0",
        "default": false
      }
    ],
    // 是否展示
    "sys_show_hide": [
      {
        "createBy": 1,
        "createTime": "2023-09-29 11:47:28",
        "updateBy": null,
        "updateTime": null,
        "params": {
          "@type": "java.util.HashMap"
        },
        "dictCode": 4,
        "dictSort": 1,
        "dictLabel": "显示",
        "dictValue": "0",
        "dictType": "sys_show_hide",
        "cssClass": "",
        "listClass": "primary",
        "isDefault": "Y",
        "status": "0",
        "remark": "显示菜单"
      },
      {
        "createBy": 1,
        "createTime": "2023-09-29 11:47:28",
        "updateBy": null,
        "updateTime": null,
        "params": {
          "@type": "java.util.HashMap"
        },
        "dictCode": 5,
        "dictSort": 2,
        "dictLabel": "隐藏",
        "dictValue": "1",
        "dictType": "sys_show_hide",
        "cssClass": "",
        "listClass": "danger",
        "isDefault": "N",
        "status": "0",
        "remark": "隐藏菜单"
      }
    ]
  }
/**
 *
 * @returns {Promise<unknown>}
 */
function install() {
  Vue.use(DataDict, {
    metas: {
      '*': {
        labelField: 'dictLabel',
        valueField: 'dictValue',
        request(dictMeta) {
          let dictData = dictDataMap[dictMeta.type];
          if (dictData) {
            return new Promise(resolve => {
              resolve(dictData)
            })
          } else {
            return new Promise((resolve, reject) => {
              getDicts(dictMeta.type).then(res => {
                store.dispatch('dict/setDict', {key: dictMeta.type, value: res.data})
                resolve(res.data)
              }).catch(error => {
                reject(error)
              })
            })
          }
        },
      },
    },
  })
}

export default {
  install,
}
