import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Dashboard.vue'
import { randomString } from '../lib/helpers'

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/container/:container/session',
    name: 'new-session',
    redirect: to => {
      const container = to.params.container
      let session = localStorage.getItem(container)

      if (!session) {
        session = randomString()
        localStorage.setItem(container, session)
      }

      return { name: 'session', params: { container, session } }
    }
  },
  {
    path: '/container/:container/session/:session',
    name: 'session',
    component: () => import('../views/Session.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
