import { ESHOP_SET_I_TOKEN_CART, ESHOP_SET_I_TOKEN_REDEEM_LATER_STATUS } from '@/store/utils/types'
import _ from 'lodash'

export const addITokenRedemptionProduct = async ({ commit, dispatch, getters }, payload) => {
  let cartItems = _.cloneDeep(getters.getITokenCartItems)
  let startIndex = cartItems.findIndex(product => product.id === payload.id && product.type === payload.type)

  if (startIndex !== -1) {
    if (payload.quantity > 0) {
      cartItems[startIndex].quantity = payload.quantity
    } else {
      cartItems.splice(startIndex, 1)
    }
  } else if (payload.quantity > 0) {
    cartItems.push(payload)
  }
  await dispatch('updateITokenRedemptionCart', cartItems)
}

export const updateITokenRedemptionCart = ({ commit }, products) => {
  commit(ESHOP_SET_I_TOKEN_CART, products)
}

export const updateITokenRedeemLaterStatus = ({ commit }, status) => {
  commit(ESHOP_SET_I_TOKEN_REDEEM_LATER_STATUS, status)
}
