import Vue from 'vue';
import Router from 'vue-router';
import pages from '../pages';

Vue.use(Router);

export function createRouter() {
  const routes = pages.map(page => {
    const route = Object.assign({}, page);

    route.path = `/grass${page.path}`;
    return route;
  });

  const route = new Router({
    mode: 'history',
    routes,
    scrollBehavior(to, from, savedPosition) {
      if (savedPosition) {
        return savedPosition;
      } else {
        return { x: 0, y: 0 };
      }
    }
  });

  route.beforeEach((to, from, next) => {
    if (!to.matched.length) {
      return next({name: 'error.404'});
    }
    next();
  });

  return route;
}