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

const requireComponent = require.context(
  // Look for files in the current directory
  '../app',
  // Look in  subdirectories
  true,
  // Only include "_base-" prefixed .vue files
  /router.js$/
)

let subRoutes = []
// For each matching file name...
requireComponent.keys().forEach(fileName => {
  // Can use _ to turn route off
  if (fileName.indexOf('_') === -1) {
    subRoutes = [...subRoutes, ...requireComponent(fileName).default]
  }
})

Vue.use(Router)

const routes = [...subRoutes, { path: '*', component: notFound }]

const router = new Router({
  mode: localStorage.getItem('ROUTE_MODE') ? localStorage.getItem('ROUTE_MODE') : 'hash',
  linkActiveClass: 'open active',
  routes: routes,
})

// 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
