import { createApp } from 'vue'
import App from './ui/App.vue'
import router from './ui/router'
import axios from "axios"
import { library } from '@fortawesome/fontawesome-svg-core'
import { fas } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'


axios.interceptors.request.use(function (config) {
    const token = localStorage.getItem("token");
    config.headers.Authorization =  `Bearer ${token}`;
    return config;
});

function rootUrl() {
  return process.env.NODE_ENV === 'development'
    ? 'http://localhost:8089'
      : '';
}


axios.defaults.baseURL = rootUrl();

axios.interceptors.response.use(function (response) {
    return response;
  }, function (error) {
      console.log(error);
      if (error.response.status === 426) {
        router.push("/setup");
      }
      if (error.response.status === 401) {
        router.push("/login");
      }
      return error;
  });


library.add(fas)

const rootUrlPlugin = {
  install: (app) => {
    app.config.globalProperties.$rootUrl = rootUrl
  }
};


createApp(App)
    .use(router)
    .component("font-awesome-icon", FontAwesomeIcon)
    .use(rootUrlPlugin)
    .mount('#app')
