import { THIRD_PARTY_APP_CHECKOUT_COMPLETE } from '@/config/url-prefix'
import { uuidv4 } from '@/helpers/hash'
import appSettings from '@@/config/app-settings'

export default {
  name: 'thirdPartyAppsCheckoutAutoship',
  type: 'path',
  match: [THIRD_PARTY_APP_CHECKOUT_COMPLETE],
  inAppArea: async function(app) {
    let identity = app.$store.getters.getUserIdentifier

    if (identity.trim() === '') {
      identity = uuidv4()
    }
    if (
      !app.$store.getters.isLoggedIn &&
      app.$store.getters.isOrderComplete &&
      appSettings.thirdParty.byDzyneApp.chekout.enrolmentCheckoutAutoship
    ) {
      await app.$store.dispatch('3rdPartyCheckoutAutoship/setIsAutoship', true)
      await app.$store.dispatch('setUserIdentifier', { type: 'autoship', value: identity })
    }
  },
  outAppArea: async function(app) {
    await app.$store.dispatch('3rdPartyCheckoutAutoship/setIsAutoship', false)
    await app.$store.dispatch('clearUserIdentifier')
  },
}
