import { reactive } from 'vue'
import { getDict } from '@/hooks/web/useDictionaries'

const dictObj = reactive({})
const dictList = [
  { 
    type: 'positionData', 
    apiType: 'positionData', 
    key: 'positionId', 
    label: 'position', 
    value: 'positionTypeData', 
    itemKey: 'id', 
    itemText: 'nameCn'
  },
  { 
    type: 'industryList', 
    apiType: 'industryList', 
    key: 'industryIdList', 
    label: 'industry', 
    isArray: true, 
    value: 'industryTypeData', 
    itemKey: 'id', 
    itemText: 'nameCn' 
  },
  { 
    type: 'menduner_area_type',
    apiType: 'areaList',
    key: 'workAreaId',
    label: 'workArea',
    value: 'areaTypeData',
    itemKey: 'id',
    itemText: 'name'
  },
  { 
    type: 'menduner_job_type',
    key: 'jobType',
    label: 'jobTypeName',
    value: 'jobTypeData',
    itemKey: 'value',
    itemText: 'label'
  }
]

// 字典
const getDictList = async () => {
  dictList.forEach(async (val) => {
    const { data } = await getDict(val.type, val.params, val.apiType)
    dictObj[val.value] = data
  })
}

const getData = async () => {
  await getDictList()
}
getData()

export const dealJobData = (list) => {
  let res = {}
  dictList.forEach(item => {
    res = list.map(e => {
      let obj = {}
      if (item.isArray) {
        e[item.label] = e[item.key].map(val => {
          return obj = dictObj[item.value].find(i => i[item.itemKey] === val)
        })
      } else {
        obj = dictObj[item.value].find(k => Number(k[item.itemKey]) === Number(e[item.key]))
        if (!obj) return
        e[item.label] = obj[item.itemText]
      }
      return e
    })
  })
  return res
}