import type { RouteRecordRaw } from "vue-router"
import ProductOrchestrator from "../ProductOrchestrator.vue"
import useAuthentication from "@racletteOrchestrator/composables/useAuthentication"
import { $store } from "@racletteCore"

const routes: Readonly<RouteRecordRaw[]> = [
  {
    path: "/:pageId(.*)",
    name: "app.space.page",
    props: true,
    component: ProductOrchestrator,
  },
  {
    path: "/logout",
    name: "public.logout",
    component: { template: "<div />" },
    beforeEnter: () => {
      const { clearCookies } = useAuthentication()

      clearCookies()
      
      // Clear project context so a later socket re-join cannot light-rejoin on stale state.
      $store.dispatch({ type: "projectInit" })
      $store.dispatch({ type: "project/set", payload: { _id: null } })

      if (window.location.href !== window.location.origin + "/login") {
        window.location.href = window.location.origin + "/login"
      }
    },
  },
] as const

export default routes
