import { createRouter, createWebHistory } from 'vue-router'
import Chat from '@/views/chat/Index.vue'
import Login from '@/views/login/Index.vue'
import { useSessionStorage } from '@vueuse/core'
import * as AuthUtils from '@/utils/auth'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Chat',
      component: Chat,
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
    },
  ]
})



router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    // check auth
    if (AuthUtils.getAccessToken()) {
      next()
    } else {
      next({path: '/login'})
    }
  } else {
    next()
  }
})

export default router
