import { getTypeCode, getRegion } from '@/api/dictionary.js'
import { strFirstUpper } from '@/utils/index.js'
import constantData from './constantData.js'
import { treeChildrenEmpty } from '@/utils'

export default {
  state: {
    noConstant: ['regionList'],
    tree: [],
    noCache: [],
    params: [],
    constant: {
      ...constantData, // 前端维护的常量数据
      projectInitiationStatus: [],
      salesContractPersonType: [],
      amountKind: [],
      invoiceType: [],
      InvoiceStatus: [],
      ledgerStatus: [],
      regionList: [], // 省市区
      factoringProducts: [] // 我的融资 - 还款计划 - 产品类型
    }
  },
  mutations: {
    SEY_DICT(state, { type, data }) {
      state.constant[type] = data
    }
  },
  actions: {
    getConstant({ dispatch, state }, name) {
      const events = []
      const isConstant = name => {
        if (state.noConstant.includes(name)) {
          events.push(dispatch('get' + strFirstUpper(name), name))
        } else if (state.tree.includes(name)) {
          events.push(dispatch('getTreeDict', name))
        } else if (typeof (name) === 'object' && state.params.includes(name.type)) {
          events.push(dispatch('get' + strFirstUpper(name.type), name))
        } else {
          events.push(dispatch('getDict', name))
        }
      }
      if (Array.isArray(name)) {
        for (const item of name) {
          var type = typeof (item) === 'object' ? item.type : item
          if (!state.noCache.includes(type)) {
            const constantType = Object.prototype.toString.call(state.constant[type])
            if (constantType === '[object Array]') {
              if (state.constant[type]?.length) continue
            } else if (constantType === '[object Object]') {
              if (Object.keys(state.constant[type]).length) continue
            } else if (constantType === '[object String]') {
              if (state.constant[type]) continue
            }
          }
          isConstant(item)
        }
      } else {
        var typeStr = typeof (name) === 'object' ? name.type : name
        if (!state.noCache.includes(typeStr)) {
          if (!state.noCache.includes(typeStr)) {
            const typeStr = Object.prototype.toString.call(state.constant[typeStr])
            if (typeStr === '[object Array]') {
              if (state.constant[typeStr].length) return
            } else if (typeStr === '[object Object]') {
              if (Object.keys(state.constant[typeStr]).length) return
            } else if (typeStr === '[object String]') {
              if (state.constant[typeStr]) return
            }
          }
        }
        isConstant(name)
      }
      return Promise.all(events)
    },
    getDict({ commit }, type) {
      return new Promise((resolve, reject) => {
        getTypeCode({ typeCode: type }).then(({ data }) => {
          commit('SEY_DICT', { type, data })
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    getRegionList({ commit }, type) {
      return new Promise((resolve, reject) => {
        getRegion().then(({ data }) => {
          commit('SEY_DICT', { type, data: treeChildrenEmpty(data) })
          resolve()
        })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}
