import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/pages/layout'
import Map from '@/pages/map'
import List from '@/pages/list'
import Home from '@/pages/home'
import Search from '@/pages/search'
import External from '@/pages/External'

Vue.use(Router)
let vue = new Vue()
let isLocal = vue.$isLocal
// let isLocal = process.env.isLocal
// let isLocal = Vue.$isLocal

let routes = [
  {
    path: '/',
    name: 'mainLayout',
    component: Layout,
    children: [
      {
        path: '/',
        name: 'home',
        component: Home
      },
      {
        path: '/list/:title/:type',
        name: 'list',
        component: List,
        children: [
          {name: 'item', path: ':show'}
        ]
      }
    ]
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  },
  {
    path: '/external/:url/:id',
    name: 'external',
    component: External
  }
]

isLocal || routes[0].children.push(
  {
    path: '/map/:title',
    name: 'map',
    component: Map,
    children: [
      {name: 'info', path: ':show'}
    ]
  }
)

const router = new Router({
  routes,
  scrollBehavior (to, from, savedPosition) {
    if (to.hash === '') {
      return { x: 0, y: 0 }
    } else {
      return false
    }
  }
})

global.paths = []
router.beforeEach((to, from, next) => {
  global.paths.push(from)
  next()
})

global.setExternalUrl = (url) => {
  router.push({name: 'external', params: {url: url}})
}

export default router
