/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */
import { 
    GET_CART_PRODUCTS,
    ADD_TO_CART,
    REMOVE_FROM_CART,
    REDUCE_QUANTITY,
    RAISE_QUANTITY,
    TOTAL_CART_VALUE,
    CLEAR_CART,
    QUANTITY_UPDATE
} from "../types/Cart";

const INITIAL_STATE : HomePageReducerState = {
    cartProducts: [],
    cartItemNumber: 0,
    cartValue: 0,
    productQuntity:{}
  };

  interface HomePageReducerState {
    cartProducts: Array<any>;
    cartItemNumber: number;
    cartValue: number;
    productQuntity: {[key:string]:number};
  }
  
  export default function (state = INITIAL_STATE, action:any): HomePageReducerState {
    switch (action.type) {
      case QUANTITY_UPDATE:
        return {...state, productQuntity:action.payload}
      case GET_CART_PRODUCTS:
        return { ...state, cartProducts: action.payload, cartItemNumber: action.payload.length };
      case ADD_TO_CART:
        return { ...state, cartProducts: action.payload, cartItemNumber: action.payload.length };
      case REMOVE_FROM_CART:
        return { ...state, cartProducts: action.payload, cartItemNumber: action.payload.length };
      case REDUCE_QUANTITY:
        return { ...state, cartProducts: action.payload };
      case RAISE_QUANTITY:
        return {...state, cartProducts: action.payload};
      case TOTAL_CART_VALUE:
        return {...state, cartValue: action.payload};
      case CLEAR_CART:
        return {...state, cartProducts: [], cartItemNumber: 0, productQuntity: {}, cartValue: 0};
      default :
        return state
    }
  };