import {instanceDiy, instanceReal, instanceThreeReal} from "@/utils/http"; // 导入 Axios 实例
import { AxiosRequestConfig, AxiosInstance } from "axios";

// 定义 API 响应数据结构
export interface ApiResponse<T> {
  code: number; // 响应状态码
  message: string; // 响应消息
  data: T; // 响应数据
}

// 扩展 Axios 请求配置，添加自定义属性 requireToken
export interface CustomRequestConfig extends AxiosRequestConfig {
  requireToken?: boolean; // 是否需要 token
  Tenant?: string; // 店铺id
}
interface RequestOptions<R> {
  url: string;
  params?: R;
  config?: CustomRequestConfig;
  data?: R;
}
// 创建请求方法
const createRequestMethods = (http: AxiosInstance) => {
  // 发起 GET 请求
  const get = async <T, R = Record<string, any>>(
    options: RequestOptions<R>,
  ) => {
    const { url, params, config } = options;
    const response = await http.get<ApiResponse<T>>(url, { params, ...config });
    return response.data as T; // 返回响应数据
  };

  // 发起 POST 请求
  const post = async <T, R = Record<string, any>>(
    options: RequestOptions<R>,
  ) => {
    const { url, data, config } = options;
    const response = await http.post<ApiResponse<T>>(url, data, config);
    return response.data as T; // 返回响应数据
  };

  // 发起 PUT 请求
  const put = async <T, R = Record<string, any>>(
    options: RequestOptions<R>,
  ) => {
    const { url, data, config } = options;
    const response = await http.put<ApiResponse<T>>(url, data, config);
    return response.data as T; // 返回响应数据
  };

  // 发起 DELETE 请求
  const del = async <T, R>(options: RequestOptions<R>) => {
    const { url, config } = options;
    const response = await http.delete<ApiResponse<T>>(url, config);
    return response.data as T; // 返回响应数据
  };

  // 返回封装的请求方法
  return { get, post, put, del };
};

// 创建实例化后的请求方法对象，用于真实环境
export const realApi = createRequestMethods(instanceReal);

export const mallApi = createRequestMethods(instanceDiy);

export const threeRealApi = createRequestMethods(instanceThreeReal);
