import * as T from '@/store/utils/types'
import { APP_AUTOSHIP_FORM } from '@@/store/localStorageKeys'
import { SET_AUTOSHIP_FORM, SET_IS_AUTOSHIP } from '@@/store/types'
import _ from 'lodash'

let defaultAutoshipForm = {
  autoshipBillOn: null,
  autoshipBillingCycle: { value: 1, text: 'Monthly' },
}

export default {
  namespaced: true,
  state: {
    [APP_AUTOSHIP_FORM]: JSON.parse(sessionStorage.getItem(APP_AUTOSHIP_FORM)) || defaultAutoshipForm,
    isAutoShip: false,
  },
  getters: {
    getAutoshipInfo: state => {
      return state[APP_AUTOSHIP_FORM]
    },
    isAutoShip: state => {
      return state.isAutoShip
    },
  },
  actions: {
    [APP_AUTOSHIP_FORM]({ commit }, payload) {
      commit(SET_AUTOSHIP_FORM, payload)
    },
    setIsAutoship({ commit }, payload = false) {
      commit(SET_IS_AUTOSHIP, payload)
    },
  },
  mutations: {
    [SET_IS_AUTOSHIP](state, status) {
      state.isAutoShip = status
    },
    [SET_AUTOSHIP_FORM](state, data) {
      state[APP_AUTOSHIP_FORM] = _.cloneDeep(data)
    },
    [T.CLEAR_LOADED_DATA](state, type) {
      if (type === 'autoship' || !type) {
        state[APP_AUTOSHIP_FORM] = defaultAutoshipForm
        sessionStorage.removeItem(APP_AUTOSHIP_FORM)
      }
    },
  },
}
