import { AppContextState, AppContextAction } from '../common/types';

export function appReducer(state: AppContextState, action: AppContextAction) {
  switch (action.type) {
    case 'content/set':
      return {
        ...state,
        content: {
          component: action.payload,
        },
      };
    case 'ticket/add': {
      return {
        ...state,
        sell: {
          ticketTypes: [...state.sell.ticketTypes, action.payload],
          finalPrice: state.sell.finalPrice + Number(action.payload.price),
        },
      };
    }
    case 'ticket/remove': {
      const newTicketTypes = state.sell.ticketTypes.filter(
        ({ id }) => id !== action.payload.id
      );
      return {
        ...state,
        sell: {
          ticketTypes: newTicketTypes,
          finalPrice: state.sell.finalPrice - Number(action.payload.price),
        },
      };
    }
    default:
      return state;
  }
}
