import { attachPostRequestListener } from '../../request';
import apiEndpoints from '../../request/apiEndpoints';
import { logoutUser } from '../actions/login/loginActions';

// Interceptor unique name
export const requestStatusMiddlewareInterceptorName = "REQUEST_STATUS_MIDDLEWARE_INTERCEPTOR";

export default ({ dispatch }) => (next) => (action) => {
  attachPostRequestListener((error) => {
    if (!error.response) {
      return Promise.reject(error);
    }
    if (
      error.response.config.url !== apiEndpoints.authentications.login &&
      error.response.config.url !==
        apiEndpoints.authentications.confirmSecurityQuestion &&
      error.response.status === 4003
    ) {
      return dispatch(logoutUser());
    }
    return Promise.reject(error);
  }, requestStatusMiddlewareInterceptorName);

  next(action);
};
