import Vue from 'vue'
import VueRouter from 'vue-router'
import {
    isUserLoggedIn
} from "@/auth/utils";
import {
    canNavigate
} from '@/libs/acl/routeProtection';
import {
    settings,
} from './config'

Vue.use(VueRouter)



const router = new VueRouter(settings)
router.beforeEach((to, from, next) => {
    if(to.meta.breadcrumb && to.meta.breadcrumb[1] && to.meta.breadcrumb[1].to) {
        to.meta.breadcrumb[1].to = `/activityPage/${to.params.activity_id}`
    }
    
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isUserLoggedIn()) {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        } else if (!canNavigate(to)) {
            next({
                path: '/not-authorized',
            })
        } else {
            next()
        }
    } else {
        next()
    }
})
// ? For splash screen
// Remove afterEach hook if you are not using splash screen
router.afterEach(() => {
    // Remove initial loading
    const appLoading = document.getElementById('loading-bg')
    if (appLoading) {
        appLoading.style.display = 'none'
    }
})



export default router