import { getFailureActionType, getRequestActionType, getSuccessActionType } from '../../../utils';

function getErrorReducer(types) {
  const requestActions = types.map(type => getRequestActionType(type));
  const successActions = types.map(type => getSuccessActionType(type));
  const failureActions = types.map(type => getFailureActionType(type));
  return function errorReducer(state = null, action) {
    if (requestActions.includes(action.type) || successActions.includes(action.type)) return null;
    else if (failureActions.includes(action.type)) return action.payload;
    else return state;
  };
}

export default getErrorReducer;
