/**
 * Request Adapter Interface
 *
 * 定义统一的请求接口，抽象平台差异
 */

export interface RequestConfig {
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
  data?: any;
  params?: Record<string, any>;
  headers?: Record<string, string>;
  timeout?: number;
  responseType?: 'json' | 'text' | 'blob' | 'arraybuffer';
}

export interface RequestResponse<T = any> {
  data: T;
  status: number;
  statusText: string;
  headers: Record<string, string>;
}

export interface RequestInterceptor {
  onRequest?: (config: RequestConfig) => RequestConfig | Promise<RequestConfig>;
  onRequestError?: (error: any) => any;
  onResponse?: <T = any>(
    response: RequestResponse<T>
  ) => RequestResponse<T> | Promise<RequestResponse<T>>;
  onResponseError?: (error: any) => any;
}

export interface IRequestAdapter {
  /**
   * 发送请求
   */
  request<T = any>(config: RequestConfig): Promise<RequestResponse<T>>;

  /**
   * GET 请求
   */
  get<T = any>(
    url: string,
    params?: Record<string, any>,
    config?: Partial<RequestConfig>
  ): Promise<RequestResponse<T>>;

  /**
   * POST 请求
   */
  post<T = any>(
    url: string,
    data?: any,
    config?: Partial<RequestConfig>
  ): Promise<RequestResponse<T>>;

  /**
   * PUT 请求
   */
  put<T = any>(
    url: string,
    data?: any,
    config?: Partial<RequestConfig>
  ): Promise<RequestResponse<T>>;

  /**
   * DELETE 请求
   */
  delete<T = any>(url: string, config?: Partial<RequestConfig>): Promise<RequestResponse<T>>;

  /**
   * 添加请求拦截器
   */
  addInterceptor(interceptor: RequestInterceptor): void;

  /**
   * 设置基础 URL
   */
  setBaseURL(baseURL: string): void;

  /**
   * 设置默认请求头
   */
  setHeader(key: string, value: string): void;
}
