import { types } from "../actions/actionTypes";

const initialState = {
  checkoutData: {},
  taxInfosData: {},
  infoForCheckoutData: {},
  taxAndSHData: {},
  checkoutTotalData: {},
  selectedCoupons: {},
  checkoutOrderData: {},
};

export const checkoutReducer = (state = initialState, action) => {
  switch (action.type) {
    case types.GET_CHECKOUT_INFO:
      return {
        ...state,
        checkoutData: action.payload,
      };

    case types.GET_TAX_INFOS:
      return {
        ...state,
        taxInfosData: action.payload,
      };

    case types.GET_CHECKOUT_TOTAL_AMT:
      return {
        ...state,
        checkoutTotalData: action.payload,
      };

    case types.INSERT_MY_INFO_CHECKOUT_ORDER:
      return {
        ...state,
        checkoutOrderData: action.payload,
      };

    case types.UPDATE_SELECTED_SHIPPING_ADDR:
      return {
        ...state,
        infoForCheckoutData: {
          ...state.infoForCheckoutData,
          dlvrAddrSno: action.payload,
        },
      };

    case types.UPDATE_SELECTED_BILLING_ADDR:
      return {
        ...state,
        infoForCheckoutData: {
          ...state.infoForCheckoutData,
          bilAddrSno: action.payload,
        },
      };

    case types.UPDATE_SELECTED_COUPON:
      return {
        ...state,
        selectedCoupons: {
          ...state.selectedCoupons,
          [action.payload.productId]: action.payload.coupon,
        },
      };

    case types.CHECKOUT_DATA_RESET:
      return initialState;

    default:
      return state;
  }
};
