import { mapGetters, mapActions } from 'vuex'
import { CurrencyMixin, mapMasterDataId } from '@/app/_global/mixins'
import _ from 'lodash'
import Routes from '../../router'

export default {
  mixins: [CurrencyMixin],
  data() {
    return {
      shippingModel: {},
      currentDeliveryMethod: null,
      routes: Routes[0].children,
      hasSubscription: true,
    }
  },
  provide() {
    let self = this
    return {
      _general_selected_country_id: function() {
        return self.getAppLocationCountry.countryId
      },
    }
  },
  methods: {
    orderProps: function() {
      return {
        orderItem: this.mappedCartItems,
        currencySymbol: this.currency,
        summaryCount: this.summaryCount,
        summaryAmount: this.summaryAmount,
        grandTotal: this.getSubscriptionSale.order_fees.total_gmp,
        continueText: this.$t('Esac.EsacVoucherRedemption.Button.Continue'),
        showNoteForFocPwpItems: this.getCartPromotions.length > 0,
        showItemImage: true,
        showItemCv: true,
        showEsacVoucher: false,
        esacVoucherItem: [],
        showEwallet: true,
        ewalletAmount: 0,
        showPromoCode: false,
        promoCode: '',
        applyPromoCode: null,
        continueDisabled: false,
      }
    },
  },
  computed: {
    ...mapGetters('eshop', [
      'getCartItems',
      'getCartTotal',
      'getCartCv',
      'getCartTotalPriceRoundingAdjustmentFormatted',
      'getCartAllTotalPrices',
      'getCartPrice',
      'getCartTotalNpmFormatted',
      'getCartTotalAdminFeeFormatted',
      'getCartTotalDeliveryFeeFormatted',
      'getCartTotalTaxFormatted',
      'getCartTotalPriceFormatted',
      'getCartPromotions',
      'getProductOrKittingDetails',
    ]),
    ...mapGetters([
      'getSubscriptionSale',
      'getSubscriptionProductsAndKitting',
      'getAppLocationCountry',
      'getSubscriptionTax',
    ]),
    mappedCartItems() {
      let self = this
      let cartItems = _.map(self.getSubscriptionProductsAndKitting, function(item) {
        return {
          item_name: item.name,
          item_image: item.image || null,
          item_code: item.sku || item.code,
          quantity: item.quantity,
          amount:
            self.$helpers.getValue(item, 'base_price.total', false) ||
            self.$helpers.getValue(item, 'base_price.gmp_price_tax', '0'),
          cv: self.$helpers.getValue(item, 'base_price.base_cv', '0'),
          free: parseFloat(self.$helpers.getValue(item, 'base_price.total')) === 0,
        }
      })
      return _.union(cartItems)
    },
    getCartQuery() {
      return {
        countryId: this.countryId,
        locationId: this.locationId,
      }
    },
    getTotalItems() {
      let totalQty = this.getCartTotal
      totalQty += this.getCartPromotions.reduce((carry, item) => {
        return carry + item.quantity
      }, 0)
      return totalQty
    },
    summaryCount() {
      return [
        { label: this.$t('Eshop.Order.Summary.Items'), value: this.mappedCartItems.length },
        { label: this.$t('Common.Terminology.TotalBV'), value: 0 },
      ]
    },
    summaryAmount() {
      return [
        { label: this.$t('Common.Sales.TotalNetExcludesGST'), value: this.getSubscriptionSale.order_fees.total_nmp },
        { label: this.$t('Common.Sales.AdminFee'), value: this.getSubscriptionSale.order_fees.admin_fee },
        { label: this.$t('Common.Sales.DeliveryFee'), value: this.getSubscriptionSale.order_fees.delivery_fee },
        { label: this.$t('Common.Sales.TaxAmount'), value: this.getSubscriptionTax },
        {
          label: this.$t('Common.Sales.RoundingAdjustmentAmount'),
          value: this.getSubscriptionSale.order_fees.rounding_adjustment,
        },
      ]
    },
  },
}
