import { ADD_ALL_DECKS_TO_CART, ADD_DECK_TO_CART, REMOVE_DECK_FROM_CART } from '../constants/cart';
import { getSuccessActionType } from '../utils';
import { FETCH_ORDER } from '../constants/order';
import { CREATE_PAYMENT } from '../constants/payment';

function reducer(state = [], action) {
  switch (action.type) {
    case ADD_DECK_TO_CART:
      return [...state.filter(({ id }) => action.payload.id !== id), action.payload.deck];
    case REMOVE_DECK_FROM_CART:
      return state.filter(({ id }) => action.payload.deckId !== id);
    case ADD_ALL_DECKS_TO_CART:
      return action.payload.decks.filter(deck => deck.price !== 0);
    case getSuccessActionType(CREATE_PAYMENT):
      return [];
    case getSuccessActionType(FETCH_ORDER):
      return action.payload.status && action.payload.price === 0 ? [] : state;

    default:
      return state;
  }
}

export default reducer;
