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

export const wwwAPI = "/qwilight/www";

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

wwwAXIOS.interceptors.response.use(
  (value) => value,
  ({ response }: AxiosError) => {
    switch (response?.status) {
      case 401:
        if (!(response.data as { isSilent?: boolean }).isSilent) {
          toast.error("Unauthorized");
          window.sessionStorage.removeItem("totem");
          window.location.reload();
        }
        break;
      case 413:
        toast.error("Payload Too Large");
        break;
      case 502:
        toast.error("Bad Gateway");
        break;
    }
  },
);
