import { createRouter, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import { getVersionFR } from '@/utils/version'
import layout from '@/components/layout/index.vue'
declare module 'vue-router' {
  interface RouteMeta {
    title?: string,
    transition?:any
  }
}
const routes: Array<RouteRecordRaw> = [
  {
    // 路由初始指向
    path: '/',
    name: 'redirect',
    component: layout,
    meta: {
      transition: 'van-fade'
    },
    redirect: 'home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/home.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'cate',
        name: 'cate',
        component: () => import('@/views/category.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'search',
        name: 'search',
        component: () => import('@/views/search.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'settings',
        name: 'settings',
        component: () => import('@/views/settings.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'my_game',
        name: 'my_game',
        component: () => import('@/views/mine.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/order.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'login',
        name: 'login',
        component: () => import('@/views/login.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'coin_type1',
        name: 'coin_type1',
        component: () => import('@/views/ncoin.vue'),
        meta: {
          transition: 'van-fade'
        }
      },
      {
        path: 'coin_type2',
        name: 'coin_type2',
        component: () => import('@/views/ecoin.vue'),
        meta: {
          transition: 'van-fade'
        }
      }
    ]
  }
]
const router = createRouter({
  // createWebHashHistory hash 路由
  // createWebHistory history 路由
  // createMemoryHistory 带缓存 history 路由
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  }
})
const whteList = ['/home', '/login', '/my_game', '/cate', '/']
router.beforeEach((to, _from, next) => {
  // ...
  // 返回 false 以取消导航
  // return false

  const token = sessionStorage.getItem('token')
  if (whteList.includes(to.path)) {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      next()
    }
  }
})
router.afterEach(() => {
  getVersionFR()
})

export default router
