import * as T from '../../utils/types'
import Vue from 'vue'
import { getValue } from '@/helpers/variable-handling'

export default {
  [T.UPDATE_CARD_LIST](state, payload) {
    state.cardList = []
    if (payload.length > 0) {
      state.cardList.push({ value: null, text: Vue.prototype.$t('Card.ChooseCard.Label'), disabled: true })
      for (let i in payload) {
        state.cardList.push({
          value: payload[i],
          text: `${payload[i].card_type} ${payload[i].card_number} Exp:${payload[i].card_expiry}`,
        })
      }
    }
  },

  [T.SET_SHIP_CYCLE](state, payload) {
    state.autoshipShippingCycle = {
      billOn: payload.billOnDate,
      billingCycle: payload.billingCycle,
    }
  },

  [T.SET_PAYMENT_INFO](state, payload) {
    state.autoshipPaymentInfo = payload
  },

  [T.SET_SHIPPING_INFO](state, payload) {
    state.autoshipShippingInfo = payload
  },

  [T.SET_BILLING_INFO](state, payload) {
    state.autoshipBillingInfo = payload
  },

  [T.SET_AUTOSHIP_SUBSCRIPTION_LIST](state, payload) {
    state.subscriptionList = []
    for (let i in payload) {
      state.subscriptionList.push(payload[i])
    }
  },
  [T.SET_AUTOSHIP_SUBSCRIPTION_DETAIL](state, payload) {
    if (typeof getValue(payload, 'shipping_address.pickup_address.recipient_addresses') === 'string') {
      try {
        payload.shipping_address.pickup_address.recipient_addresses = JSON.parse(
          payload.shipping_address.pickup_address.recipient_addresses
        )
      } catch {
        payload.shipping_address.pickup_address.recipient_addresses = []
      }
    }
    state.subscriptionDetail = payload
  },

  [T.SET_SUBSCRIPTION_LIST_PAGINATION_META](state, payload) {
    state.subscriptionListPaginationMeta = payload.pagination
  },

  [T.SET_AUTOSHIP_SEARCH_FILTER](state, data) {
    state.autoshipSearchFilter = data
  },

  [T.CLEAR_LOADED_DATA](state, type) {
    if (type === 'autoship' || !type) {
      state.autoshipShippingCycle = {}
      state.autoshipPaymentInfo = null
      state.autoshipBillingInfo = {}
      state.autoshipShippingInfo = {}
    }
  },

  [T.AUTOSHIP_DATA](state, data) {
    state.autoshipData = data
  },
}
