import { getFailureActionType, getRequestActionType, getSuccessActionType } from '../../../utils';
import { ERROR, INIT, LOADING, OK } from '../../../constants';

function getStatusReducer(types) {
  const requestActions = types.map(type => getRequestActionType(type));
  const successActions = types.map(type => getSuccessActionType(type));
  const failureActions = types.map(type => getFailureActionType(type));
  return function statusReducer(state = INIT, action) {
    if (requestActions.includes(action.type)) {
      return LOADING;
    } else if (successActions.includes(action.type)) {
      return OK;
    } else if (failureActions.includes(action.type)) {
      return ERROR;
    } else {
      return state;
    }
  };
}

export default getStatusReducer;
