// import {singleModels} from "../enums/single-model-enums"
// import multiModels from '../enums/multi-model-enums'
import { getViewToken } from '@/api/model'
import { getAccessToken } from '@/api/login'
import { setToken } from '@/utils/jsCookie'
import axios from "axios"

// 基于public配置文件获取
const getSingleModelViewTokenList = async(name?: string) => {
  return new Promise(async (resolve, reject) =>{
    let viewTokenArray: any = []
    axios.get('/data.json').then(async(res) => {
      let singleModels = res.data.singleModels
      let obj: any = name ? (name === 'WaterWarmElect' && singleModels.length > 1 ? [singleModels[1]] : [singleModels[0]]) : singleModels
      for(let model of obj){
        let res: any = await getViewToken(model.value)
        if(res.data.code === 'success'){
          viewTokenArray.push(res.data.data)
        } else if (res.code =='authentication.failed') {
          getAccessToken().then((res) => {
            if(res.data.code == 'success'){
              setToken(res.data.data.token)
            }
          })
        }
      }
      const tokenName = name ? (name === 'WaterWarmElect' ? 'electModelViewTokenList' : 'singleModelViewToken') : 'singleModelViewTokenList'
      await window.sessionStorage.setItem(tokenName, viewTokenArray)
      resolve(true)
    })
    
  })
}

// const getSingleModelViewTokenList = async(name?: string) => {
//   return new Promise(async (resolve, reject) =>{
//     let viewTokenArray: any = []
//     let obj: any = name ? (name === 'WaterWarmElect' ? [singleModels.values[1]] : [singleModels.values[0]]) : singleModels.values
//     for(let model of obj){
//       let res: any = await getViewToken(model.value)
//       if(res.data.code === 'success'){
//         viewTokenArray.push(res.data.data)
//       } else if (res.code =='authentication.failed') {
//         getAccessToken().then((res) => {
//           if(res.data.code == 'success'){
//             setToken(res.data.data.token)
//           }
//         })
//       }
//     }
//     const tokenName = name ? (name === 'WaterWarmElect' ? 'electModelViewTokenList' : 'singleModelViewToken') : 'singleModelViewTokenList'
//     await window.sessionStorage.setItem(tokenName, viewTokenArray)
//     resolve(true)
//   })
// }


// 使用基于data.json的数据
const getMultiModelViewTokenList = () => {
  return new Promise(async (resolve, reject) =>{
    axios.get('/data.json').then(async(res) => {
      let multiModels = res.data.multiModels
      let viewTokenArray: any = []
      for(let model of multiModels){
        let res: any = await getViewToken(model.value)
        if(res.data.code === 'success'){
          viewTokenArray.push(res.data.data)
        } else if (res.code =='authentication.failed') {
          getAccessToken().then((res) => {
            if(res.data.code == 'success'){
              setToken(res.data.data.token)
            }
          })
        }
      }
      window.sessionStorage.setItem('multiModelViewTokenList', viewTokenArray)
      resolve(true)
    })
  })
}


// const getMultiModelViewTokenList = () => {
//   return new Promise(async (resolve, reject) =>{
//     let viewTokenArray: any = []
//     for(let model of multiModels.values){
//       let res: any = await getViewToken(model.value)
//       if(res.data.code === 'success'){
//         viewTokenArray.push(res.data.data)
//       } else if (res.code =='authentication.failed') {
//         getAccessToken().then((res) => {
//           if(res.data.code == 'success'){
//             setToken(res.data.data.token)
//           }
//         })
//       }
//     }
//     window.sessionStorage.setItem('multiModelViewTokenList', viewTokenArray)
//     resolve(true)
//   })
// }

export {
  getSingleModelViewTokenList,
  getMultiModelViewTokenList
}