import type { InternalAxiosRequestConfig, AxiosResponse } from 'axios'
import { ResultEnum } from '@/enums/HttpEnum'
import { useUserStoreWithOut } from '@/stores'
const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
  const userStore = useUserStoreWithOut()
  if (config.headers && typeof config.headers.set === 'function') {
    config.headers.set('Authorization', 'Bearer ' + userStore.token)
  }
  return config
}

const defaultResponseInterceptors = (response: AxiosResponse) => {
  const { code, msg } = response.data
  if (response?.config?.responseType === 'blob') {
    // 如果是文件流，直接过
    return response
  } else if (code === ResultEnum.SUCCESS) {
    return response.data
  } else {
    console.error(msg)
    ElMessage.error(msg)
  }
}
export { defaultResponseInterceptors, defaultRequestInterceptors }
