// src/api/axiosInstance.ts

import axios from "axios";
import axiosRetry from "axios-retry";

import { API_URL } from "./config";

import {
  requestInterceptor,
  responseSuccessInterceptor,
  responseErrorInterceptor,
} from "./interceptors";

const axiosInstance = axios.create({
  baseURL: API_URL,
  timeout: 10000,
  headers: {
    "Content-Type": "application/json",
  },
});

axiosInstance.interceptors.request.use(
  requestInterceptor
);

axiosInstance.interceptors.response.use(
  responseSuccessInterceptor,
  responseErrorInterceptor
);

axiosRetry(axiosInstance, {
  retries: 3,
  retryDelay: axiosRetry.exponentialDelay,

  retryCondition: (error) =>
    axiosRetry.isNetworkOrIdempotentRequestError(
      error
    ) ||
    error.response?.status === 429,
});

export default axiosInstance;