import { fetchBaseQuery, retry } from "@reduxjs/toolkit/query/react";
import { getDefaultLanguage } from "i18n/helper";
import { ACCESS_TOKEN } from "utils/constants";

const baseQuery = ({ baseUrl }: { baseUrl: string }) =>
  retry(
    fetchBaseQuery({
      baseUrl,
      validateStatus: (response, result) => {
        return response.status?.toString()[0] === "2" && !result.error;
      },
      prepareHeaders: (headers) => {
        const token = localStorage.getItem(ACCESS_TOKEN);

        headers.set("Accept", "application/json");
        headers.set("Accept-Language", getDefaultLanguage());

        if (token) headers.set("authorization", `Bearer ${token}`);

        return headers;
      },
    }),
    {
      maxRetries: 0, // Set the maximum number of retries to 3
    }
  );
export default baseQuery;
