import { createAxiosInstance } from "./axios";

export type Response<T> = {
  errorCode: string;
  message: string;
  msg: string;
  status: boolean;
  data: T;
};

export const authApiInstance = createAxiosInstance(
  import.meta.env.VITE_API_AUTH_URL
);

export const productApiInstance = createAxiosInstance(
  import.meta.env.VITE_API_PRODUCT_URL
);

export class BaseApi {
  protected baseUrl: string;
  protected defaultTimeout = 10000; // 10 giây

  constructor(baseUrl: string, timeout?: number) {
    this.baseUrl = baseUrl;
    if (timeout) {
      this.defaultTimeout = timeout;
    }
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  async productPost<T>(
    endpoint: string = "",
    data?: any,
    timeout?: number
  ): Promise<Response<T>> {
    return productApiInstance.post(`${this.baseUrl}${endpoint}`, data, {
      timeout: timeout ?? this.defaultTimeout,
    });
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  async authPost<T>(
    endpoint: string = "",
    data?: any,
    timeout?: number
  ): Promise<Response<T>> {
    return authApiInstance.post(`${this.baseUrl}${endpoint}`, data, {
      timeout: timeout ?? this.defaultTimeout,
    });
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  async authGet<T>(
    endpoint: string = "",
    timeout?: number
  ): Promise<Response<T>> {
    return authApiInstance.get(`${this.baseUrl}${endpoint}`, {
      timeout: timeout ?? this.defaultTimeout,
    });
  }
}
