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

export default {
  mixins: [CurrencyMixin],
  data() {
    return {
      shippingModel: {},
      currentDeliveryMethod: null,
    }
  },
  provide() {
    let self = this
    return {
      _general_selected_country_id: function() {
        return self.countryId
      },
      _shipping_methods_allow_save_shipping_address: function() {
        return false
      },
    }
  },
  props: {
    next: {
      type: Function,
    },
    countryId: {
      type: Number,
      required: true,
    },
    locationId: {
      type: Number,
      required: true,
    },
  },
  methods: {
    ...mapActions('eshop', ['saveOrderShipping', 'loadCart']),
    orderProps: function() {
      return {
        orderItem: this.mappedCartItems,
        currencySymbol: this.currency,
        summaryCount: this.summaryCount,
        summaryAmount: this.summaryAmount,
        grandTotal: this.getCartTotalPriceFormatted,
        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,
      }
    },
    getOnHoldPaymentKey() {
      return [this.getCartHasRental ? 'ek_smart_on_hold_payment_options' : 'general_on_hold_payment_options']
    },
  },
  computed: {
    ...mapMasterDataId({
      SALE_DELIVERY_METHOD: {
        deliveryMethodId: 'DELIVERY',
      },
      PROMOTION_FREE_ITEM_PROMO_TYPES: {
        focId: 'PROMO_TYPE_FOC',
        pwpfId: 'PROMO_TYPE_PWPF',
        pwpnId: 'PROMO_TYPE_PWPN',
      },
    }),
    ...mapGetters('eshop', [
      'getCartItems',
      'getCartTotal',
      'getCartCv',
      'getCartTotalPriceRoundingAdjustmentFormatted',
      'getCartAllTotalPrices',
      'getCartPrice',
      'getCartTotalNpmFormatted',
      'getCartTotalAdminFeeFormatted',
      'getCartTotalDeliveryFeeFormatted',
      'getCartTotalTaxFormatted',
      'getCartTotalPriceFormatted',
      'getCartPromotions',
      'getProductOrKittingDetails',
      'getCartHasRental',
    ]),
    mappedCartItems() {
      let cartItems = _.map(this.getCartItems, function(item) {
        return {
          item_name: item.name,
          item_image: item.image,
          item_code: item.sku,
          quantity: item.quantity,
          amount: item.total_price,
          cv: item.total_cv.eligible,
          free: item.free || false,
        }
      })
      let self = this
      let promoItems = _.map(this.getCartPromotions, function(item) {
        return {
          item_name: item.name,
          item_image: '/static/products/img-placeholder.svg',
          item_code:
            item.promo_type_id === self.focId ? self.$t('Eshop.Order.FocPwp.FOC') : self.$t('Eshop.Order.FocPwp.PWP'),
          products: item.products,
          quantity: item.quantity,
          amount: item.total_price,
          cv: 0,
          free: item.promo_type_id === self.focId,
        }
      })
      return _.union(cartItems, promoItems)
    },
    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.getTotalItems || 0 },
        { label: this.$t('Common.Terminology.TotalBV'), value: this.getCartCv.eligible || 0 },
      ]
    },
    summaryAmount() {
      return [
        { label: this.$t('Common.Sales.TotalNetExcludesGST'), value: this.getCartTotalNpmFormatted },
        { label: this.$t('Common.Sales.AdminFee'), value: this.getCartTotalAdminFeeFormatted },
        { label: this.$t('Common.Sales.DeliveryFee'), value: this.getCartTotalDeliveryFeeFormatted },
        { label: this.$t('Common.Sales.TaxAmount'), value: this.getCartTotalTaxFormatted },
        {
          label: this.$t('Common.Sales.RoundingAdjustmentAmount'),
          value: this.getCartTotalPriceRoundingAdjustmentFormatted,
        },
      ]
    },
  },
  watch: {
    shippingModel() {
      let shippingModel = _.cloneDeep(this.shippingModel)
      shippingModel.validate = true
      this.saveOrderShipping(shippingModel)
      if (
        shippingModel.sale_delivery_method === this.deliveryMethodId ||
        this.currentDeliveryMethod !== shippingModel.sale_delivery_method
      ) {
        this.loadCart(this.getCartQuery)
      }
      this.currentDeliveryMethod = shippingModel.sale_delivery_method
    },
  },
}
