import axios, { AxiosError, isAxiosError } from "axios";
import { toast } from "react-toastify";

export const wwwAPIPath = "/www";

export const wwwAPI = axios.create({
  baseURL: wwwAPIPath,
});

wwwAPI.interceptors.response.use(
  (value) => value,
  (e: AxiosError) => {
    switch (e.response?.status) {
      case 401:
        window.sessionStorage.removeItem("totem");
        window.location.reload();
        break;
      case 413:
        toast.error("Payload Too Large");
        break;
      case 502:
        toast.error("Bad Gateway");
        break;
    }

    throw e;
  },
);

export const isClientFault = (e: Error) =>
  isAxiosError(e) && e.response?.status === 403;
