import { mapActions } from 'vuex'

export default {
  data() {
    return {
      bundleConfig: {
        priceTypes: [],
        enrolmentTypes: [],
      },
    }
  },
  methods: {
    ...mapActions(['loadHierarchicalConfig']),
    async loadBundleConfig() {
      if (this.$appSettings.bundle.dynamicPrice.enable) {
        await Promise.all([
          this.loadHierarchicalConfig('bundle_price_config'),
          this.loadHierarchicalConfig('bundle_member_price'),
        ]).then(res => {
          this.bundleConfig = {
            priceTypes: this.bundleConfigDataMapper(this.$helpers.getValue(res, '[0].children', [])),
            enrolmentTypes: this.bundleConfigDataMapper(this.$helpers.getValue(res, '[1].children', [])),
          }
        })
      }
    },
    bundleConfigDataMapper(data = []) {
      return data.map(option => {
        let { key, normalized_id, normalized_value, mapping_id, mapping_model } = option
        return { key, normalized_id, normalized_value, mapping_id, mapping_model }
      })
    },
    hasConfig(key, configs = []) {
      return configs.findIndex(config => config.key === key) !== -1
    },
    getBundleCheckoutEnrolmentKey(configs = []) {
      return (configs.find(config => !['bundle_member_price', 'bundle_retail_price'].includes(config.key)) || {}).key
    },
  },
}
