import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import staticRoutes from '/@/router/static'
import { adminBaseRoutePath } from '/@/router/static/adminBase'
import { loading } from '/@/utils/loading'
import langAutoLoadMap from '/@/lang/autoload'
import { mergeMessage } from '/@/lang/index'
import { useConfig } from '/@/stores/config'
import { useAdminInfo } from '/@/stores/adminInfo'
import { isAdminApp } from '/@/utils/common'
import { uniq } from 'lodash-es'

const router = createRouter({
    history: createWebHashHistory(),
    routes: staticRoutes,
})

router.beforeEach((to, from, next) => {
    // 后台路由鉴权：未登录时访问 /admin 或 /admin/*（除 /admin/login）则跳转登录页
    const isAdminRoute = to.path === adminBaseRoutePath || to.path.startsWith(adminBaseRoutePath + '/')
    const isAdminLogin = to.path === adminBaseRoutePath + '/login'
    if (isAdminRoute && !isAdminLogin) {
        const adminInfo = useAdminInfo()
        if (!adminInfo.token) {
            next({ name: 'adminLogin' })
            return
        }
    }

    NProgress.configure({ showSpinner: false })
    NProgress.start()
    if (!window.existLoading) {
        loading.show()
        window.existLoading = true
    }

    // 按需动态加载页面的语言包-start
    let loadPath: string[] = []
    const toCamelPath = (path: string): string => {
        return path
            .split('/')
            .map((seg) => seg.replace(/_([a-z])/g, (_m, c: string) => c.toUpperCase()))
            .join('/')
    }
    const config = useConfig()
    const langCode = config.lang.defaultLang === 'zh' ? 'zh-cn' : config.lang.defaultLang
    if (to.path in langAutoLoadMap) {
        loadPath.push(...langAutoLoadMap[to.path as keyof typeof langAutoLoadMap])
    }
    let prefix = ''
    if (isAdminApp(to.fullPath)) {
        prefix = './backend/' + langCode

        // 去除 path 中的 /admin
        const adminPath = to.path.slice(to.path.indexOf(adminBaseRoutePath) + adminBaseRoutePath.length)
        if (adminPath) {
            loadPath.push(prefix + adminPath + '.ts')
            loadPath.push(prefix + toCamelPath(adminPath) + '.ts')
        }
    } else {
        prefix = './frontend/' + langCode
        loadPath.push(prefix + to.path + '.ts')
    }

    // 根据路由 name 加载的语言包
    if (to.name) {
        const routeName = to.name.toString()
        loadPath.push(prefix + '/' + routeName + '.ts')
        loadPath.push(prefix + '/' + toCamelPath(routeName) + '.ts')
    }

    if (!window.loadLangHandle.publicMessageLoaded) window.loadLangHandle.publicMessageLoaded = []
    const publicMessagePath = prefix + '.ts'
    if (!window.loadLangHandle.publicMessageLoaded.includes(publicMessagePath)) {
        loadPath.push(publicMessagePath)
        window.loadLangHandle.publicMessageLoaded.push(publicMessagePath)
    }

    // 去重
    loadPath = uniq(loadPath)

    for (const key in loadPath) {
        loadPath[key] = loadPath[key].replaceAll('${lang}', langCode)
        if (loadPath[key] in window.loadLangHandle) {
            window.loadLangHandle[loadPath[key]]().then((res: { default: anyObj }) => {
                const pathName = loadPath[key].slice(loadPath[key].lastIndexOf(prefix) + (prefix.length + 1), loadPath[key].lastIndexOf('.'))
                mergeMessage(res.default, pathName)
            })
        }
    }
    // 动态加载语言包-end

    next()
})

// 路由加载后
router.afterEach(() => {
    if (window.existLoading) {
        loading.hide()
    }
    NProgress.done()
})

export default router
