import { APP_SHIPPING_AND_PAYMENT_FORM } from '@@/store/localStorageKeys'
import { SET_APP_SHIPPING_AND_PAYMENT_FORM } from '@@/store/types'
import _ from 'lodash'
import * as T from '@/store/utils/types'

let defaultShippingAndPaymentForm = {
  billingInfo: {},
  shippingInfo: {},
}

export default {
  namespaced: true,
  state: {
    [APP_SHIPPING_AND_PAYMENT_FORM]:
      JSON.parse(sessionStorage.getItem(APP_SHIPPING_AND_PAYMENT_FORM)) || defaultShippingAndPaymentForm,
  },
  getters: {
    getShippingAndPaymentInfo: state => {
      return state[APP_SHIPPING_AND_PAYMENT_FORM]
    },
  },
  actions: {
    [APP_SHIPPING_AND_PAYMENT_FORM]({ commit }, payload) {
      commit(SET_APP_SHIPPING_AND_PAYMENT_FORM, payload)
    },
  },
  mutations: {
    [SET_APP_SHIPPING_AND_PAYMENT_FORM](state, data) {
      state[APP_SHIPPING_AND_PAYMENT_FORM] = _.cloneDeep(data)
    },
    [T.CLEAR_LOADED_DATA](state, type) {
      if (type === 'shippingAndPayment' || !type) {
        state[APP_SHIPPING_AND_PAYMENT_FORM] = defaultShippingAndPaymentForm
        sessionStorage.removeItem(APP_SHIPPING_AND_PAYMENT_FORM)
      }
    },
  },
}
