import { reactive } from 'vue'
import { getDict } from '@/hooks/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',
    params: { type: undefined },
    label: 'workArea',
    value: 'areaTypeData',
    itemKey: 'id',
    itemText: 'name'
  },
  { 
    type: 'menduner_job_type',
    key: 'jobType',
    label: 'jobTypeName',
    value: 'jobTypeData',
    itemKey: 'value',
    itemText: 'label'
  },
  { 
    type: 'menduner_area_type', 
    apiType: 'areaList', 
    key: 'interestedAreaIdList', 
    label: 'interestedArea', 
    // params: { type: undefined },
    isArray: true, 
    value: 'areaTypeData', 
    itemKey: 'id', 
    itemText: 'name' 
  }
]

// 字典
const getDictList = async () => {
  dictList.forEach(async (val) => {
    const { data } = await getDict(val.type, val.params, val.apiType)
    if (!data?.data) {
      dictObj[val.value] = []
      return
    }
    dictObj[val.value] = data.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) {
        if (e[item.key] && e[item.key].length) {
          const result = e[item.key].map(val => {
            return obj = dictObj[item.value].find(i => Number(i[item.itemKey]) === Number(val))
          })
          e[item.label] = result && result.length ? result.filter(Boolean) : []
        }
      } 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
}