import { createRouter, createWebHashHistory, Router } from 'vue-router';

const router: Router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: () => import('@/views/colexiu-technology'),
      meta: {
        title: '首页'
      }
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home'),
      meta: {
        title: '首页'
      }
    },
    {
      path: '/colexiu-person',
      name: 'colexiu-person',
      component: () => import('@/views/colexiu-person'),
      meta: {
        title: '酷乐秀个人版'
      }
    },
    {
      path: '/colexiu-tenant',
      name: 'colexiu-tenant',
      component: () => import('@/views/colexiu-tenant'),
      meta: {
        title: '酷乐秀机构版'
      }
    },
    {
      path: '/cooperate-channels',
      name: 'cooperate-channels',
      component: () => import('@/views/cooperate-channels'),
      meta: {
        title: '合作渠道'
      }
    },
    {
      path: '/music-classroom',
      name: 'music-classroom',
      component: () => import('@/views/music-classroom'),
      meta: {
        title: '音乐数字课堂'
      }
    },
    {
      path: '/colexiu-technology',
      name: 'colexiu-technology',
      component: () => import('@/views/colexiu-technology'),
      meta: {
        title: '酷乐秀科技'
      }
    },
    {
      path: '/about-us',
      name: 'about-us',
      component: () => import('@/views/about-us'),
      meta: {
        title: '关于我们'
      }
    },
    {
      path: '/:pathMatch(.*)*',
      // component: () => import('@/views/colexiu-technology'),
      redirect: '/',
      meta: {
        title: '首页'
      }
    }
  ],
  scrollBehavior(to, form, savedPosition) {
    // if (to.hash) {
    //   return {
    //     el: to.hash,
    //     behavior: 'smooth'
    //   };
    // }
    if (savedPosition) {
      return savedPosition;
    } else {
      return { left: 0, top: 0 };
    }
  }
});

router.onError(error => {
  if (error instanceof Error) {
    const isChunkLoadFailed = error.name.indexOf('chunk');
    const targetPath = router.currentRoute.value.fullPath;
    console.log(error);
    if (isChunkLoadFailed) {
      location.hash = targetPath;
      window.location.reload();
    }
  }
});

export default router;
