import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { title: '首页' }
  },
  {
    path: '/brochure',
    redirect: '/brochure/company'
  },
  {
    path: '/brochure/:topic',
    name: 'Brochure',
    component: () => import('../views/BrochurePage.vue'),
    meta: { title: '宣传册' }
  },
  {
    path: '/live',
    name: 'LiveRoom',
    component: () => import('../views/LiveRoom.vue'),
    meta: { title: '直播' }
  },
  {
    path: '/live/login',
    name: 'LiveLogin',
    component: () => import('../views/LiveLogin.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/live/register',
    name: 'LiveRegister',
    component: () => import('../views/LiveRegister.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/index',
    redirect: '/'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, _from, savedPosition) {
    if (savedPosition) return savedPosition
    if (to.hash) {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ el: to.hash, behavior: 'smooth', top: 80 })
        }, 50)
      })
    }
    return { top: 0, left: 0 }
  }
})

export default router
