import axios, { AxiosResponse, AxiosRequestConfig, Axios, AxiosError, InternalAxiosRequestConfig } from 'axios'
import { RequestHttpHeaderEnum, ResultEnum, ModuleTypeEnum } from '@/enums/httpEnum'
import { PageEnum, ErrorPageNameMap } from '@/enums/pageEnum'
import { StorageEnum } from '@/enums/storageEnum'
import { axiosPre } from '@/settings/httpSetting'
import { SystemStoreEnum, SystemStoreUserInfoEnum } from '@/store/modules/systemStore/systemStore.d'
import { redirectErrorPage, getLocalStorage, routerTurnByName, isPreview } from '@/utils'
import { fetchAllowList } from './axios.config'
import includes from 'lodash/includes'
import { refreshToken, isRefreshRequest } from '@/api/refresh.js'

export interface MyResponseType<T> {
  code: ResultEnum
  data: T
  message: string
}

export interface MyRequestInstance extends Axios {
  <T = any>(config: AxiosRequestConfig): Promise<MyResponseType<T>>
}

const axiosInstance = axios.create({
  baseURL: `${import.meta.env.PROD ? import.meta.env.VITE_PRO_PATH : ''}${axiosPre}`,
  // baseURL:`${import.meta.env.VITE_PRO_PATH}${axiosPre}`,
  timeout: ResultEnum.TIMEOUT
}) as unknown as MyRequestInstance

console.log(`${import.meta.env.PROD }`,'sssss'),
axiosInstance.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    // 白名单校验
    if (includes(fetchAllowList, config.url)) return config
    // 获取 token
    const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
    // 重新登录
    if (!info) {
      routerTurnByName(PageEnum.BASE_LOGIN_NAME)
      return config
    }
    const userInfo = info[SystemStoreEnum.USER_INFO]
    config.headers[userInfo[SystemStoreUserInfoEnum.TOKEN_NAME] || 'token'] =
      userInfo[SystemStoreUserInfoEnum.USER_TOKEN] || ''
    return config
  },
  (err: AxiosError) => {
    Promise.reject(err)
  }
)

// 响应拦截器
axiosInstance.interceptors.response.use(
  async (res: AxiosResponse) => {
    // 预览页面错误不进行处理
    if (isPreview()) {
      return Promise.resolve(res.data)
    }
    const { code } = res.data as { code: number }

    if (code === undefined || code === null) return Promise.resolve(res.data)

    // 成功
    if (code === ResultEnum.SUCCESS) {
      return Promise.resolve(res.data)
    }

    // 登录过期
    // if (code === ResultEnum.TOKEN_OVERDUE ) {
    //   console.log('token过期')
    //   window['$message'].error(window['$t']('http.token_overdue_message'))
    //   routerTurnByName(PageEnum.BASE_LOGIN_NAME)
    //   return Promise.resolve(res.data)
    // }

   

    //重新获取token
    if (code === ResultEnum.TOKEN_OVERDUE  && !isRefreshRequest(res.config)) {
      const isSucceed = await refreshToken()
      if (isSucceed) {
        const info = getLocalStorage(StorageEnum.GO_SYSTEM_STORE)
        const userInfo = info[SystemStoreEnum.USER_INFO]
        res.config.headers[userInfo[SystemStoreUserInfoEnum.TOKEN_NAME] || 'token'] =
          userInfo[SystemStoreUserInfoEnum.USER_TOKEN] || ''
        let result = await axiosInstance.request(res.config) //重新请求
        return result
      }
    }

     // 固定错误码重定向
    // if (ErrorPageNameMap.get(code)) {
    //   // redirectErrorPage(code)
    //   return Promise.resolve(res.data)
    // }

    // // 提示错误
    // window['$message'].error(window['$t']((res.data as any).msg))
    // return Promise.resolve(res.data)
  },
  (err: AxiosError) => {
    const status = err.response?.status
    switch (status) {
      case 401:
        routerTurnByName(PageEnum.BASE_LOGIN_NAME)
        Promise.reject(err)
        break

      default:
        Promise.reject(err)
        break
    }
  }
)

export default axiosInstance
