import { createAuth } from '@websanova/vue-auth';
import driverAuthBearer from '@websanova/vue-auth/dist/drivers/auth/bearer.esm.js';
import driverHttpAxios from '@websanova/vue-auth/dist/drivers/http/axios.1.x.esm.js';
import driverRouterVueRouter from '@websanova/vue-auth/dist/drivers/router/vue-router.2.x.esm.js';
import axios from 'axios';
import { createPinia } from 'pinia';
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';


import Nora from '@primevue/themes/nora';
import PrimeVue from 'primevue/config';
import ConfirmationService from 'primevue/confirmationservice';
import ToastService from 'primevue/toastservice';

import BlockViewer from '@/components/BlockViewer.vue';

import '@/assets/styles.scss';
import '@/assets/tailwind.css';
import { config } from 'md-editor-v3';

import { LoadingStore } from './stores/LoadingStore.js';





config({
    editorConfig: {
        renderDelay: 0,
        zIndex: 200000000
    }
  });
var auth = createAuth({
    plugins: {
        http: axios,
        router: router
    },
    drivers: {
        http: driverHttpAxios,
        auth: driverAuthBearer,
        router: driverRouterVueRouter
    },
    options:{
        notFoundRedirect: '/auth/login',
        authRedirect: '/auth/login',
      /*  loginData: {url: '../api/auth/login', method: 'POST', redirect: '/'},
        logoutData: {url:'../api/auth/logout', redirect: '/auth/login'},
        fetchData: {url: '../api/auth/fetch-user', method: 'GET', enabled: true},
        refreshData: {url: '../api/auth/refresh-token', method: 'GET', enabled: true}*/
        loginData: {url: '/api/auth/login', method: 'POST', redirect: '/'},
        logoutData: {url:'/api/auth/logout', redirect: '/auth/login'},
        fetchData: {url: '/api/auth/fetch-user', method: 'GET', enabled: true},
        refreshData: {url: '/api/auth/refresh-token', method: 'GET', enabled: true}
    }
});

axios.defaults.baseURL = import.meta.env.VITE_BACKEND_URL;//'http://localhost:8081'///



console.log(import.meta.env.VITE_BACKEND_URL);

const app = createApp(App);
const pinia = createPinia()

app.use(pinia);
app.use(router);

app.use(PrimeVue, {
    theme: {
        preset: Nora,
        options: {
            prefix: 'p',
            darkModeSelector: '.app-dark',
            cssLayer: false
        },
    }
});
app.use(ToastService);
app.use(ConfirmationService);
app.use(auth);

app.component('BlockViewer', BlockViewer);

app.mount('#app');

const loadingStore = LoadingStore()


axios.interceptors.request.use(function (config) {
    loadingStore.another_loading = true;
    return config
}, function (error) {
    return Promise.reject(error);
  });
  
axios.interceptors.response.use(function (response) {
    loadingStore.another_loading = false;

    return response;
  }, function (error) {
    return Promise.reject(error);
  });
