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

export default {
  state: {
    noConstant: ['regionList', 'countryList'],
    tree: [],
    noCache: [],
    params: [],
    constant: {
      ...constantData, // 前端维护的常量数据
      enterprise_type: [], // 我的企业-企业类型
      country_cod: [], // 我的企业-注册国家
      sys_yes_no: [],
      district_code: [], // 我的企业-代理公司所属区域
      certification_status: [], // 我的企业-企业认证状态
      export_type: [], // 我的企业-出口类型
      tax_authority_area: [], // 主管税务机关
      countryList: [],
      trade_terms: [], // 我的贸易-购销合同-贸易术语
      currency_code: [], // 我的贸易-购销合同-币种
      payment_way: [], // 我的贸易-购销合同-购销合同付款方式
      sales_contract_status: [], // // 我的贸易-购销合同-销售合同状态
      purchase_contract_status: [],
      invoice_type: [],
      sys_sign_status: [], // 企业签署状态
      sales_order_status: [], // 销售订单状态
      purchase_order_status: [] // 采购订单状态
    }
  },
  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)
          })
      })
    },
    getCountryList({ commit }, type) {
      return new Promise((resolve, reject) => {
        getForeignCountry().then(({ data }) => {
          commit('SEY_DICT', { type, data: data })
          resolve()
        })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}
