import * as T from '../../../utils/types'
import * as L from '../../../utils/localStorageKeys'
import _ from 'lodash'

export default {
  [T.ESHOP_CART_SET_FOC_PWP](state, data) {
    if (data.order_fees) {
      state.orderFees = data.order_fees
    }
    if (data.products) {
      state.products = data.products
    }
    if (data.kittings) {
      state.kittings = data.kittings
    }
    if (data.promotion) {
      // if the previous promotion is not same as the current promotion, reset the state
      if (!_.isEqual(state.promotion, data.promotion)) {
        state.selectedFocItems = []
        state.selectedPwpItems = []
        state.selectedFocPwpItems = []
        state.focSelectionComplete = false
        state.pwpSelectionComplete = false
        state.pwpFocSelectionComplete = false
        localStorage.setItem(L.ESHOP_FOCPWP_SELECTED_IDS, JSON.stringify(state.selectedFocPwpItems))
      }
      state.promotion = data.promotion
    }
    localStorage.setItem(L.ESHOP_FOCPWP_ELIGIBLE_PROMOTIONS, JSON.stringify(state.promotion))
  },
  [T.SET_SELECTED_FOC_ITEMS](state, data) {
    state.selectedFocItems = data
  },
  [T.SET_SELECTED_PWP_ITEMS](state, data) {
    state.selectedPwpItems = data
  },
  [T.SET_SELECTED_FOCPWP_ITEMS](state, data) {
    state.selectedFocPwpItems = data
    localStorage.setItem(L.ESHOP_FOCPWP_SELECTED_IDS, JSON.stringify(data))
  },
  [T.SET_FOC_SELECTION_COMPLETE](state, data) {
    state.focSelectionComplete = data
  },
  [T.SET_PWP_SELECTION_COMPLETE](state, data) {
    state.pwpSelectionComplete = data
  },
  [T.SET_PWP_FOC_SELECTION_COMPLETE](state, data) {
    state.pwpFocSelectionComplete = data
  },
  [T.CLEAR_LOADED_DATA](state, type) {
    if (type === 'focpwp' || !type) {
      state.selectedFocPwpItems = []
      state.promotion = []
      state.pwpFocSelectionComplete = false
      localStorage.setItem(L.ESHOP_FOCPWP_SELECTED_IDS, JSON.stringify(state.selectedFocPwpItems))
    }
  },
}
