import * as vueRouter from 'vue-router'
import baseData from './routers'
import config from '~src/config/index'
import { exists_file } from '~src/utils/tools'

//设置路由数据
const router = vueRouter.createRouter({
    history: vueRouter.createWebHashHistory(),
    routes: baseData,
})

//路由拦截
router.beforeResolve((to) => {
    //判断是否存在配置文件
    if (!exists_file(config.configPath) && to.path !== '/init') {
        return '/init'
    } else {
        return true
    }
})

router.afterEach((to) => {
    let pageTitle = to.meta['title'], title = config.title
    if (to.path !== '/') {
        window.document.title = pageTitle ? `${pageTitle} - ${title}` : title
    } else {
        window.document.title = title
    }
    window.scrollTo(0, 0)
})

export function setupRouter(app) {
    app.use(router)
    return router
}

export default router
