import i18next from "i18next";
import { attachPostRequestListener } from "../../request";
import { makeErrorToastMessage } from "../utils/makeToastMessage";

//Interceptor unique name
export const serverErrorMiddlewareInterceptorName =
  "INTERNAL_SERVER_ERROR_MIDDLEWARE_INTERCEPTOR";

export default () => (next) => (action) => {
  attachPostRequestListener((error) => {
    if (!error.response) {
      return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong"));
    }
    if (error.response.status === 500) {
      return makeErrorToastMessage(i18next.t("apiErrors.somethingWentWrong"));
    }
    return Promise.reject(error);
  }, serverErrorMiddlewareInterceptorName);

  next(action);
};
