import Vue from 'vue'
import Router from 'vue-router'
import appStatus from '@/router/app-status'

const requireComponent = require.context(
  // Look for files in the current directory
  '../app',
  true,
  /router.js$/
)

let routes = []
// For each matching file name...
requireComponent.keys().forEach(fileName => {
  if (fileName.indexOf('_') === -1) {
    routes = [...routes, ...requireComponent(fileName).default]
  }
})

Vue.use(Router)

const router = new Router({
  base: process.env.NODE_ENV === 'production' && process.env.VUE_APP_PUBLIC_PATH ? process.env.VUE_APP_PUBLIC_PATH : '',
  linkActiveClass: 'open active',
  routes: routes,
  // mode: 'history',
  scrollBehavior() {
    return { x: 0, y: 0 }
  },
})

// Capture route transition
router.beforeEach((to, from, next) => {
  // Set the app statuses of the application
  appStatus(router.app, to, from).then(() => {
    return next()
  })
})

export default router
