import { get, post } from '@/store/utils/api'
import { BUNDLE_INFO_APP_BUNDLE_DETAILS, BUNDLE_INFO_APP_CHECKOUT_BUNDLE } from '@AppCenter/store/utils/apiUrls'
import { PROCESSING_STARTED, PROCESSING_FINISHED } from '@AppCenter/store/utils/type'

export default {
  actions: {
    async getBundleInfo({ commit }, payload) {
      commit(PROCESSING_STARTED, 'loadingBundleInfo', { root: true })
      let query = `?identifier=${payload.identifier}`
      return await get(BUNDLE_INFO_APP_BUNDLE_DETAILS + payload.bundleId + query)
        .then(response => response && response.data)
        .catch(() => false)
        .finally(() => {
          commit(PROCESSING_FINISHED, 'loadingBundleInfo', { root: true })
        })
    },

    async bundleCheckout({ commit }, payload) {
      commit(PROCESSING_STARTED, 'processingBundleCheckout', { root: true })
      payload = {
        bundle_slug: payload.bundleSlug,
        quantity: payload.quantity,
      }
      return post(BUNDLE_INFO_APP_CHECKOUT_BUNDLE, payload)
        .then(response => response && response.data)
        .catch(() => false)
        .finally(() => {
          commit(PROCESSING_FINISHED, 'processingBundleCheckout', { root: true })
        })
    },
  },
}
