import {
  getDictData,
  getIndustryListData,
  getSkillList,
  getIndustryTreeData,
  getAreaListData,
  getAreaMapData,
  getPositionTreeData,
  getAreaTreeData,
  getPositionData
} from '@/api/common'
import { getSecondNodes } from '@/utils/dealData'

// const setDict = (type, val, cacheTime = 7200) => {
const setDict = (type, val) => {
  if (!val) {
    return
  }
  if (type === 'areaTreeData') {
    const obj = val.data.find(e => e.name === '中国')
    val.data = obj?.children ? obj.children.map(e =>{
      // 市辖区直接显示区
      const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
      if (municipality && e.children[0].children?.length) e.children = e.children[0].children
      return e
    }) : []
  }
  if (type === 'areaTreeDataExtend') { // 前排加上不限
    const obj = val.data.find(e => e.name === '中国')
    if (obj?.children?.length) {
      // const province = 
      obj.children.forEach(e=> {
        // 市辖区直接显示区
        const municipality = e.children && e.children.length && e.children[0].name === '市辖区'
        if (municipality && e.children[0].children?.length) e.children = e.children[0].children
        // 不限
        extendFun(e)
      })
      val.data = obj.children
    } else val.data = []

    function extendFun (e) {
      const idType = Number.isInteger(e.id) ? 'int' : 'str'
      if(e.children?.length) {
        e.children.unshift({ name: '不限', id: e.id+'unlimited', idType })
        e.children.forEach(i => extendFun(i))
      }
    }
  }
  // 一小时过期
  const currentTime = new Date()
  currentTime.setTime(currentTime.getTime() + 3600 * 1000)
  
  uni.setStorageSync(type, JSON.stringify({
    data: val,
    // expire: Date.now() + cacheTime * 1000
    expire: currentTime.getTime()
  }))
}

export const getDict = (type, params, apiType = 'dict') => {
    if (!type) {
      return []
    }
    return new Promise((resolve) => {
      const item = uni.getStorageSync(type)
      const catchData = item ? JSON.parse(item) : null
      if (catchData && catchData.expire && (Date.now() <= catchData.expire) && catchData.data && catchData.data?.data?.length) {
        return resolve({ data: catchData.data })
      }
      // 传参按照规范参数传
      const query = params ? params : { type }
      const apiFn = {
        dict: getDictData,
        positionTreeData: getPositionTreeData, // 职位tree
        areaTreeData: getAreaTreeData, // 区域tree
        areaTreeDataExtend: getAreaTreeData, // 区域tree（二级以后含不限）
        industryTreeData: getIndustryTreeData, // 行业tree
        industryList: getIndustryListData,
        skillList: getSkillList,
        areaList: getAreaListData,
        areaMap: getAreaMapData,
        positionSecondData: getPositionTreeData,
        positionData: getPositionData
      }
      apiFn[apiType](query).then(data => {
        // setDict(type, data, Date.now())
        if (type === 'positionSecondData') {
          data.data = getSecondNodes(data.data)
        }
        setDict(type, data)
        resolve({ data })
      })
    })
}