import type { App } from 'vue'

import { pages, subPackages } from '@/pages.json'
import { useUserStore } from '@/stores/modules/userStore'
// 处理分包路由
const subPages = []
subPackages.forEach((item) => {
  if (item.pages && Array.isArray(item.pages)) {
    item.pages.forEach((p) => {
      subPages.push({
        actions: p.actions,
        path: `${item.root}/${p.path}`,
      })
    })
  }
})
// 需要拦截的页面
const loginBlacklist = new Set(
  [...pages, ...subPages]
    .filter((p) => p.actions && p.actions.includes('login'))
    .map((p) => `/${p.path}`)
)

// console.log('loginBlacklist', loginBlacklist)

const interceptor = {
  invoke({ url }) {
    const userStore = useUserStore()
    const token = userStore.token
    const path = url.split('?')[0]

    const isNeedLogin = loginBlacklist.has(path)
    // 不需要登录权限的，直接跳转
    if (!isNeedLogin) {
      return true
    }
    // 已经登录的，直接跳转
    if (token) {
      return true
    }

    // 访问的是需要登录权限才能查看的页面，先跳转到提示页面提醒用户
    const redirectRoute = `/pages-sub/auth/index?redirect=${encodeURIComponent(url)}`
    uni.navigateTo({ url: redirectRoute })
    return false
  },
}

export function setupRouterInterceptor(app: App) {
  app.use({
    install() {
      uni.addInterceptor('navigateTo', interceptor)
      uni.addInterceptor('reLaunch', interceptor)
      uni.addInterceptor('redirectTo', interceptor)
    },
  })
}
