import { createRouter, createWebHistory } from 'vue-router'
import home from './home'
import rewards from './rewards'
import account from './account'
import register from './register'



const authed = localStorage.getItem("HASLOGIN")
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...register,
    {
      path: "/",
      // redirect: "/register",
      // component: () => import("@/view/register/Register.vue"),
      redirect: authed ? "/home" : "/register",
      component: () => import("@/view/Root.vue"),
      children: [
        ...home,
        ...rewards,
        ...account,
      ]
    },
  ],
})

router.beforeEach((to,from,next) => {
  console.log('什么屌authed',authed,'to.path',to.path)

  if (to.path === '/') {
    router.replace(authed ? '/home' : '/register')
    return
  }

  if (to.meta.requiresAuth && !authed) {
    next({ path: '/register', query: { redirect: to.fullPath } })
    return
  }

  next()
})

export default router
