/**
 * Data processing class, can be configured according to the project
 */
import type { AxiosRequestConfig, AxiosResponse } from 'axios'
import type { RequestOptions, Result } from '/#/axios'

export interface CreateAxiosOptions extends AxiosRequestConfig {
    authenticationScheme?: string
    authenticationHeader?: string
    transform?: AxiosTransform
    requestOptions?: RequestOptions
}

export abstract class AxiosTransform {
    /**
     * 注入自定义配置，在请求方法调用时
     * 允许动态修改本次请求的 config 和 options
     */
    inject?: (
        config: AxiosRequestConfig,
        options: RequestOptions,
    ) => { config: AxiosRequestConfig; options: RequestOptions }

    /**
     * @description: Process configuration before request
     * @description: Process configuration before request
     */
    beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig

    /**
     * @description: Request successfully processed
     */
    transformRequestHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any

    /**
     * @description: 请求失败处理
     */
    requestCatchHook?: (e: Error, options: RequestOptions) => Promise<any>

    /**
     * @description: 请求之前的拦截器
     */
    requestInterceptors?: (config: AxiosRequestConfig, options: CreateAxiosOptions) => AxiosRequestConfig

    /**
     * @description: 请求之后的拦截器
     */
    responseInterceptors?: (res: AxiosResponse<any>) => AxiosResponse<any>

    /**
     * @description: 请求之前的拦截器错误处理
     */
    requestInterceptorsCatch?: (error: Error) => void

    /**
     * @description: 请求之后的拦截器错误处理
     */
    responseInterceptorsCatch?: (axiosInstance: AxiosResponse, error: Error) => void
}
