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

// 基础路由
const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('@/pages/home/Index.vue'),
  },
  {
    path: '/products',
    name: 'Products',
    component: () => import('@/pages/products/Index.vue'),
  },
  {
    path: '/cases',
    name: 'Cases',
    component: () => import('@/pages/cases/Index.vue'),
  },
  {
    path: '/casesdetails',
    name: 'Casesdetails',
    component: () => import('@/pages/casesdetails/Index.vue'),
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('@/pages/about/Index.vue'),
  },
  {
    path: '/contact',
    name: 'Contact',
    component: () => import('@/pages/contact/Index.vue'),
  }
]

// 开发环境添加测试路由
if (process.env.NODE_ENV === 'development') {
  routes.push({
    path: '/scale-test',
    name: 'ScaleTest',
    component: () => import('@/pages/scale-test/Index.vue'),
  })
}

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的位置（比如浏览器前进后退），则返回到保存的位置
    // if (savedPosition) {
    //   return savedPosition
    // }
    // 否则滚动到页面顶部
    return { top: 0 }
  }
})

// 路由守卫：确保每次路由变化时重新应用缩放策略
router.afterEach((to, from) => {
  // 延迟执行，确保DOM已更新
  setTimeout(() => {
    import('@/utils/scaleManager.js').then(({ scaleManager }) => {
      scaleManager.checkAndRestore()
    })
  }, 150)
  
  // 再次检查，确保状态正确
  setTimeout(() => {
    import('@/utils/scaleManager.js').then(({ scaleManager }) => {
      scaleManager.checkAndRestore()
    })
  }, 500)
})

export default router