import { ActionType, AuthActions, OfferActions } from '../actions';
import { Offer } from '../models';

const INITIAL_STATE: Array<Offer> = [];

export function offerReducer(state = INITIAL_STATE, action: ActionType): Array<Offer> {
  switch (action.type) {
    case AuthActions.ActionKey.LOGOUT_REQUESTED:
      return INITIAL_STATE;
    case OfferActions.ActionKey.EMPLOYEE_OFFERS_FETCH_SUCCESS:
    case OfferActions.ActionKey.EMPLOYER_OFFERS_FETCH_SUCCESS:
      return action.offers;
    case OfferActions.ActionKey.NEW_APPLICATION_FAIL: {
      return state.filter((offer) => offer.id !== action.offerId);
    }
    default:
      return state;
  }
}
