import Vue from 'vue'
import VueRouter from 'vue-router'
import Frame from '../App.vue'
Vue.use(VueRouter)

export const allComponents = require.context('../components', false, /\.vue$/).keys()
const componentsRouters = []

allComponents.forEach(k => {
  const key = k.replace(/(\.\/|\.vue)/g, '')
  const o = {
    path: `${key}`,
    name: `${key}`,
    component: () => import(/* webpackChunkName: "`${key}`" */ `../components/${key}.vue`)
  }
  componentsRouters.push(o)
})

const router = new VueRouter({
  routes: [{
      path: '/',
      component: Frame,
      redirect: '/index',
      children: componentsRouters
    },{
      path: '/404',
      name: '404',
      component: () => import(/* webpackChunkName: "404" */ '../errors/404.vue')
    },
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
  ]
})

export default router
