import { createRouter, createWebHistory } from "vue-router";

import SystemManagementView from "./views/SystemManagementView.vue";
import WorkspaceView from "./views/WorkspaceView.vue";
import EntityDetailView from "./views/EntityDetailView.vue";
import TrackingPage from "../pages/screener/TrackingPage.vue";
import { findWorkspace } from "./workspaceRegistry";

export default createRouter({
  history: createWebHistory(),
  scrollBehavior(_to, _from, savedPosition) {
    return savedPosition ?? { left: 0, top: 0 };
  },
  routes: [
    { path: "/", redirect: "/workspace/emotion" },
    {
      path: "/workspace/screener/tracking",
      name: "screener-tracking",
      component: TrackingPage,
      meta: { title: "智能选股" },
    },
    {
      path: "/workspace/:workspace",
      name: "workspace",
      component: WorkspaceView,
      beforeEnter: (to) => (findWorkspace(String(to.params.workspace)) ? true : "/workspace/emotion"),
    },
    {
      path: "/system",
      name: "system",
      component: SystemManagementView,
      meta: { title: "系统管理" },
    },
    {
      path: "/market/:entityType/:identifier",
      name: "entity-detail",
      component: EntityDetailView,
      meta: { title: "行情详情" },
    },
    { path: "/:pathMatch(.*)*", redirect: "/workspace/emotion" },
  ],
});
