import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from '@/config/router.config'

try {
  const originalPush = Router.prototype.push
  Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
  }
} catch (e) {
  console.error(e)
}

Vue.use(Router)

const createRouter = () =>
  new Router({
    mode: 'history',
    base: window.__POWERED_BY_QIANKUN__ ? process.env.VUE_APP_ROUTER_BASE_URL : process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
