import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import useStore from '@/stores'
import SystemRouter from './system'

// 创建路由
const router = createRouter({
  history: createWebHistory('/opui/'),
  routes: SystemRouter as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})

// 重置路由
export const resetRouter = () => {
  const { permission } = useStore()
  permission.routes.forEach((route) => {
    const name = route.name
    if (name && router.hasRoute(name)) {
      router.removeRoute(name)
    }
  })
}

export default router
