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

/**
 * Displays an error message using react-toastify based on the type of Axios error.
 * 
 * @param {AxiosError} error - The error object returned by Axios.
 */
export const showErrorAxios = (error: AxiosError) => {
  let message = "";

  // Determine the error message based on the error code
  switch(error.code){
    case(axios.AxiosError.ERR_BAD_REQUEST):
      message = "Acceso no autorizado"; // Unauthorized access
      break;
    case(axios.AxiosError.ERR_NETWORK):
      message = "Conexión con el servidor fallida"; // Server connection failed
      break;
    default:
      message = error.message; // Default to the error message provided by Axios
      break;
  }

  // Display the error message using react-toastify
  toast.error(message, {
    position: "bottom-right", // Position the toast at the bottom-right corner
    autoClose: 1500, // Automatically close the toast after 1.5 seconds
    hideProgressBar: false, // Show the progress bar
    closeOnClick: true, // Close the toast when clicked
    pauseOnHover: false, // Do not pause the toast on hover
    draggable: true, // Allow the toast to be draggable
    progress: undefined, // Use the default progress bar behavior
    theme: "colored" // Use the colored theme for the toast
  });
}