import axios from "axios";

const CDNCallAxios = axios.create({
  baseURL: "http://localhost:4000",
  withCredentials: true,
});

CDNCallAxios.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => Promise.reject(error),
);

CDNCallAxios.interceptors.response.use(
  (res) => res,
  (error) => Promise.reject(error),

  // async (error: AxiosError) => {
  //   const originalRequest: any = error.config;
  //   if (
  //     error.response?.status === 503 &&
  //     error.response?.data?.error?.description === "denied" &&
  //     !originalRequest?._retry
  //   ) {

  //     originalRequest._retry = true;

  //     try {
  //       await axios.post(
  //         `${API_URL}/auth/logout`,
  //         {},
  //         {withCredentials: true}
  //       );

  //       processQueue(null);
  //       return privateAxios(originalRequest);
  //     } catch (refreshError) {
  //       processQueue(refreshError);
  //       window.location.href = "/login";
  //       return Promise.reject(refreshError);
  //     } finally {
  //       isRefreshing = false;
  //     }
  //   }
  //   if (
  //     error.response?.status === 401 &&
  //     error.response?.data?.error?.description === "unauthorized" &&
  //     !originalRequest?._retry
  //   ) {
  //     if (isRefreshing) {
  //       return new Promise((resolve, reject) => {
  //         failedQueue.push({resolve, reject});
  //       }).then(() => privateAxios(originalRequest));
  //     }

  //     originalRequest._retry = true;
  //     isRefreshing = true;

  //     try {
  //       await axios.post(
  //         `${API_URL}/auth/refresh-token`,
  //         {},
  //         {withCredentials: true}
  //       );

  //       processQueue(null);
  //       return privateAxios(originalRequest);
  //     } catch (refreshError) {
  //       processQueue(refreshError);
  //       window.location.href = "/login";
  //       return Promise.reject(refreshError);
  //     } finally {
  //       isRefreshing = false;
  //     }
  //   }

  //   return Promise.reject(error);
  // }
);

const CDNCall = {
  get: CDNCallAxios.get,
  post: CDNCallAxios.post,
  put: CDNCallAxios.put,
  patch: CDNCallAxios.patch,
  delete: CDNCallAxios.delete,
};

export default CDNCall;
