import {
  AUTOSHIP_SELECT_PRODUCT,
  AUTOSHIP_SELECT_SIZES,
  AUTOSHIP_PAYMENT_INFO,
  AUTOSHIP_SHIPPING_INFO,
  AUTOSHIP_SUMMARY,
} from '@/config/url-prefix'
import validatedRouteMixin from '@/app/_global/mixins/validatedRouteMixin'
import { mapActions, mapGetters } from 'vuex'
import _ from 'lodash'

export default {
  mixins: [validatedRouteMixin],
  data() {
    let self = this
    return {
      routingOrder: [
        {
          id: 1,
          path: AUTOSHIP_SELECT_PRODUCT + '/' + 1,
          match: AUTOSHIP_SELECT_PRODUCT,
          previous: this.$t('Common.Back'),
          next: this.$t('Common.Next'),
          validate: () => {
            return this.$route.path.indexOf(AUTOSHIP_SELECT_PRODUCT) === -1
              ? Promise.resolve(self.getCartTotal > 0 && self.getCheckout.status === true)
              : true
          },
        },
        {
          id: 2,
          path: AUTOSHIP_SELECT_SIZES,
          previous: this.$t('Common.Back'),
          next: this.$t('Common.Sales.PreCheckout'),
          addCallback: self.cartNeedSelection,
        },
        {
          id: 3,
          path: AUTOSHIP_PAYMENT_INFO,
          previous: this.$t('Common.Back'),
          next: this.$t('Common.Sales.PreCheckout'),
          validate: () => {
            return Promise.resolve(!(_.isEmpty(self.getPaymentInfo) && _.isEmpty(self.getShippingCycleDetail)))
          },
        },
        {
          id: 4,
          path: AUTOSHIP_SHIPPING_INFO,
          previous: this.$t('Common.Back'),
          next: this.$t('Common.Next'),
          validate: async () => {
            await self.loadCart()
            return Promise.resolve(
              !(_.isEmpty(self.getAutoshipShippingInfo) && _.isEmpty(self.getAutoshipBillingInfo)) &&
                self.getAutoshipShippingInfo.validated === true &&
                self.getAutoshipBillingInfo.validated === true
            )
          },
        },
        {
          id: 5,
          path: AUTOSHIP_SUMMARY,
          previous: this.$t('Common.Back'),
          next: this.$t('Common.Next'),
          complete: this.$t('Common.Confirm'),
        },
      ],
    }
  },
  methods: {
    ...mapActions('eshop', ['cartNeedSelection', 'loadCart']),
  },
  computed: {
    ...mapGetters('eshop', ['getCartTotal', 'getCheckout', 'getCartNeedSizeSelection']),
    ...mapGetters(['getShippingCycleDetail', 'getPaymentInfo', 'getAutoshipShippingInfo', 'getAutoshipBillingInfo']),
  },
}
