import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'
import store from './../store'
import {getStep} from "../helper";

const router = createRouter({
  history: createWebHistory(APP_BASE_ROUTE),
  routes: routes
})

function isAuthenticated() {
  return store.getters['auth/authenticated'];
}

router.beforeEach((to, from, next) => {
  /*if (!isAuthenticated() && to.matched.some(record => record.meta.requires_auth)) {
    next({ name: 'login' })
  } else if (isAuthenticated() && to.matched.some(record => record.meta.is_auth_route)) {
    next({ name: 'dashboard' })
  }

  next()*/
    const step = getStep();
   // console.clear()
   // console.log(from.path,to.path)
    if(to.name == "add-profile"){
        if(!step){
            next({name:'create-account'})
        }else if(step == 3){
            next({name:'choose-package'})
        }else if(step == 4){
            next({name:'create-cr-account'})
        }
    }else if(to.name == "choose-package"){
        if(!step){
            next({name:'create-account'})
        }else if(step == 2){
            next({name:'add-profile'})
        }else if(step == 4){
            next({name:'create-cr-account'})
        }
    }else if(to.name == "create-account") {
        if(step == 2){
            next({name:'add-profile'})
        }else if(step == 3){
            next({name:'choose-package'})
        }else if(step == 4){
            next({name:'create-cr-account'})
        }
    }else if(to.name == "success"){
        if(!step){
            next({name:'create-account'})
        }
    }else if(to.name == "create-cr-account"){
        if(!step){
            next({name:'create-account'})
        }else if(step == 2){
            next({name:'add-profile'})
        }else if(step == 3){
            next({name:'choose-package'})
        }else if(step == 5){
            next({name:'make-payment'})
        }
    }else if(to.name == "make-payment"){
        if(!step){
            next({name:'create-account'})
        }else if(step == 2){
            next({name:'add-profile'})
        }else if(step == 3){
            next({name:'choose-package'})
        }else if(step == 4){
            next({name:'create-cr-account'})
        }
    }

    next()

})

export default router
