import Vue from 'vue'
import VueI18n from 'vue-i18n'
import App from './App.vue'
import Keycloak from 'keycloak-js'
import config from './config.json'
import LeanISPComponents from './leanisp'
import pt from '@/assets/locale/pt.json'
import es from '@/assets/locale/es.json'
import en_US from '@/assets/locale/en-US.json'
import './registerServiceWorker'

Vue.use(LeanISPComponents, {})
Vue.use(VueI18n);

const i18n = new VueI18n({
  locale: localStorage.language || navigator.language.replace('-','_'),
  fallbackLocale: 'pt',
  messages: { pt, es, en_US },
})

Vue.config.productionTip = false

config.keycloak.url = process.env.VUE_APP_KEYCLOAK_ADDRESS
let keycloak = Keycloak(config.keycloak)

keycloak.init({onLoad: config.keycloak.onLoad}).then( auth => {
  if (!auth) {
    window.location.reload()
  } else {
    new Vue({
      i18n,
      render: h => h(App, { props : {keycloak: keycloak}})
    }).$mount('#app')
  }
}).catch(() => {
  alert("Failed to initialize the application. Try reload this page!")
  console.log("Failed to initialize keyclock! =(")
})

Vue.prototype.$keycloak = keycloak