import axios, { AxiosInstance, AxiosResponse } from 'axios'
import { RequestConfig, RequestInterceptors } from './types'
import { storage } from '@/utils'
import { constant } from '@/config'
import router from '@/router'
class Request {
  // axios实例
  instance: AxiosInstance

  // 拦截器对象
  interceptorsObj?: RequestInterceptors

  constructor(config: RequestConfig) {
    // 创建实例
    this.instance = axios.create(config)

    // 类请求拦截器
    this.instance.interceptors.request.use(
      (req: any) => {
        const token = storage.getItem(constant.CONFIG_TOKEN)
        console.log(token);
        if (token) {
          // 如果有token给请求头加上
          req.headers.Authorization = `Bearer ${token}`
          req.timeout = constant.CONFIG_REQUEST_TIMEOUT_TIME
        }
        return req
      },
      (err: any) => {

      }
    )

    // 类响应拦截器
    this.instance.interceptors.response.use(
      (res: AxiosResponse) => {
        const { data: resData } = res
        if (resData.code !== 200) {
          ElMessage.error(resData.message)
        }
        return resData
      },
      (err: any) => {
        // 根据自己业务/接口返回做相应调整
        const { status } = err.response

        switch (status) {
          case 401:
            // @ts-ignore
            ElMessage.warning('登录信息已失效，请关闭窗口，重新打开！')
            storage.removeItem('token')
            storage.removeItem('user')
            // router.push({ path: '/login' })
            break
          default:
        }
        const { Code } = err.response.data

        switch (Code) {
          case 401:
            // @ts-ignore
            ElMessage.warning('登录信息已失效，请关闭窗口，重新打开！')
            storage.removeItem('token')
            storage.removeItem('user')
            // router.push({ path: '/login' })
            break
          default:
        }
      }
    )
  }

  request<T>(config: RequestConfig): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config)
      }
      this.instance
        .request<any, T>(config)
        .then((res: T) => {
          // 如果我们为单个响应设置拦截器，这里使用单个响应的拦截器
          if (config.interceptors?.responseInterceptors) {
            res = config.interceptors.responseInterceptors<T>(res)
          }
          resolve(res)
        })
        .catch((err: any) => {
          reject(err)
        }).finally(() => {

        })
    })
  }
}

export default Request
