import axios, { type AxiosInstance, type AxiosRequestConfig } from "axios"
import { get } from "lodash-es"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { ElMessage } from "element-plus"

/** 创建请求实例 */
function createService() {
  // 创建一个 Axios 实例
  const service = axios.create()
  // 请求拦截
  service.interceptors.request.use(
    (config) => {
      return config
    },
    // 发送失败
    (error) => Promise.reject(error)
  )
  // 响应拦截（可根据具体业务作出相应的调整）
  service.interceptors.response.use(
    (response) => {
      return response
    },
    (error) => {
      if (error.response.status === 555) {
        const router = useRouter()
        const userStore = useUserStore()
        userStore.logout()
        router.push("/login").then((_) => ElMessage.warning("登录过期，请重新登录！"))
      }
      return error
    }
  )
  return service
}

/** 创建请求方法 */
function createRequestFunction(service: AxiosInstance) {
  return function <T>(config: AxiosRequestConfig): Promise<T> {
    const configDefault = {
      headers: {
        "Content-Type": get(config, "headers.Content-Type", "application/json")
      },
      timeout: 5000,
      baseURL: import.meta.env.VITE_BASE_API,
      data: {},
      widthCredentials: true
    }
    return service(Object.assign(configDefault, config))
  }
}

/** 用于网络请求的实例 */
export const service = createService()
/** 用于网络请求的方法 */
export const request = createRequestFunction(service)
