import { ENROLMENT } from '@/config/url-prefix'
import { API_GUEST_TOKEN, API_TOKEN_KEY, ENROL_SELECTED_LANGUAGE, API_TOKEN } from '@/store/utils/localStorageKeys'
import { setDefaultToken } from '@/store/utils/api'

export default {
  name: 'enrolment',
  type: 'path',
  match: [ENROLMENT],
  inAppArea: async function(app) {
    let token = localStorage.getItem(API_GUEST_TOKEN)
    localStorage.setItem(API_TOKEN_KEY, API_GUEST_TOKEN)
    let lang = JSON.parse(localStorage.getItem(ENROL_SELECTED_LANGUAGE))
    if (lang) {
      await app.$store.dispatch('enrolment/updateEnrolmentLanguage', lang)
    }

    setDefaultToken(token)
  },
  outAppArea: function(app) {
    let token = localStorage.getItem(API_TOKEN)
    localStorage.setItem(API_TOKEN_KEY, API_TOKEN)
    localStorage.removeItem(ENROL_SELECTED_LANGUAGE)

    setDefaultToken(token)
  },
}
