import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/assets/js/utils/auth' // get token from cookie
import getPageTitle from '@/assets/js/utils/get-page-title'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/authentication', '/home', '/404'] // no redirect whitelist
router.beforeEach(async (to, from, next) => {
  sessionStorage.setItem('token', 'MockToken')
  document.title = getPageTitle(to.meta.title)
  const hasToken = getToken()
  if (hasToken) {
    next()
  } else {
    if (to.path.indexOf('/home') !== -1) {
      next(`/authentication?redirect=${to.path}`)
    } else {
      if (whiteList.indexOf(to.path) !== -1) {
        next()
      } else {
        next()
        NProgress.done()
      }
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
