text: import { h, resolveComponent } from 'vue'
  import { createRouter, createWebHistory } from 'vue-router'
  
  import DefaultLayout from '@/layouts/DefaultLayout'
  import store from './../store'
  
  const ifNotAuthenticated = (to, from, next) => {
    if (!store.getters.isAuthenticated) {
      next()
      return
    }
    next('/')
  }
  
  const ifAuthenticated = (to, from, next) => {
    if (store.getters.isAuthenticated) {
      next()
      return
    }
    next('/pages/login')
  }
  
  const routes = [
    {
      path: '/',
      name: 'Home',
      component: DefaultLayout,
      redirect: '/dashboard',
      children: [
        {
          path: '/dashboard',
          name: 'Dashboard',
          beforeEnter: ifAuthenticated,
          // route level code-splitting
          // this generates a separate chunk (about.[hash].js) for this route
          // which is lazy-loaded when the route is visited.
          component: () =>
            import(/* webpackChunkName: "dashboard" */ '@/views/Dashboard.vue'),
        },
        {
          path: '/theme',
          name: 'Theme',
          redirect: '/theme/typography',
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/category',
          name: 'Category',
          component: () => import('@/views/pages/Category.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/products',
          name: 'Products',
          component: () => import('@/views/pages/Products.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/reviews',
          name: 'Reviews',
          component: () => import('@/views/pages/Reviews.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/comments',
          name: 'Reported Comments',
          component: () => import('@/views/pages/Comments.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/productdetails/:id',
          name: 'Product Details',
          component: () => import('@/views/pages/ProductDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/reviewdetail/:id',
          name: 'Review Details',
          component: () => import('@/views/pages/ReviewDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/answerdetail/:id',
          name: 'Review Details',
          component: () => import('@/views/pages/AnswerDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/questiondetail/:id',
          name: 'Question Details',
          component: () => import('@/views/pages/QuestionDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/commentdetail/:id',
          name: 'Comment Detail',
          component: () => import('@/views/pages/CommentDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/questionComment',
          name: 'Question Comments',
          component: () => import('@/views/pages/QuestionComments.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/questioncommentdetail/:id',
          name: 'Question Comment Details',
          component: () => import('@/views/pages/QuestionCommentDetail.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/reportedreviews',
          name: 'Reported Reviews',
          component: () => import('@/views/pages/ReportedReviews.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/productreports',
          name: 'Product Reports',
          component: () => import('@/views/pages/ProductReports.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/answers',
          name: 'Reported Answer',
          component: () => import('@/views/pages/Answers.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/questions',
          name: 'Questions',
          component: () => import('@/views/pages/Questions.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/pages/users',
          name: 'Users',
          component: () => import('@/views/pages/Users.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/theme/colors',
          name: 'Colors',
          component: () => import('@/views/theme/Colors.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/theme/typography',
          name: 'Typography',
          component: () => import('@/views/theme/Typography.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/base',
          name: 'Base',
          component: {
            render() {
              return h(resolveComponent('router-view'))
            },
          },
          beforeEnter: ifAuthenticated,
          redirect: '/base/breadcrumbs',
          children: [
            {
              path: '/base/accordion',
              name: 'Accordion',
              component: () => import('@/views/base/Accordion.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/breadcrumbs',
              name: 'Breadcrumbs',
              component: () => import('@/views/base/Breadcrumbs.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/cards',
              name: 'Cards',
              component: () => import('@/views/base/Cards.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/carousels',
              name: 'Carousels',
              component: () => import('@/views/base/Carousels.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/collapses',
              name: 'Collapses',
              component: () => import('@/views/base/Collapses.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/list-groups',
              name: 'List Groups',
              component: () => import('@/views/base/ListGroups.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/navs',
              name: 'Navs',
              component: () => import('@/views/base/Navs.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/paginations',
              name: 'Paginations',
              component: () => import('@/views/base/Paginations.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/popovers',
              name: 'Popovers',
              component: () => import('@/views/base/Popovers.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/progress',
              name: 'Progress',
              component: () => import('@/views/base/Progress.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/spinners',
              name: 'Spinners',
              component: () => import('@/views/base/Spinners.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/tables',
              name: 'Tables',
              component: () => import('@/views/base/Tables.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/base/tooltips',
              name: 'Tooltips',
              component: () => import('@/views/base/Tooltips.vue'),
              beforeEnter: ifAuthenticated,
            },
          ],
        },
        {
          path: '/buttons',
          name: 'Buttons',
          component: {
            render() {
              return h(resolveComponent('router-view'))
            },
          },
          beforeEnter: ifAuthenticated,
          redirect: '/buttons/standard-buttons',
          children: [
            {
              path: '/buttons/standard-buttons',
              name: 'Buttons',
              component: () => import('@/views/buttons/Buttons.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/buttons/dropdowns',
              name: 'Dropdowns',
              component: () => import('@/views/buttons/Dropdowns.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/buttons/button-groups',
              name: 'Button Groups',
              component: () => import('@/views/buttons/ButtonGroups.vue'),
              beforeEnter: ifAuthenticated,
            },
          ],
        },
        {
          path: '/forms',
          name: 'Forms',
          component: {
            render() {
              return h(resolveComponent('router-view'))
            },
          },
          beforeEnter: ifAuthenticated,
          redirect: '/forms/form-control',
          children: [
            {
              path: '/forms/form-control',
              name: 'Form Control',
              component: () => import('@/views/forms/FormControl.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/select',
              name: 'Select',
              component: () => import('@/views/forms/Select.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/checks-radios',
              name: 'Checks & Radios',
              component: () => import('@/views/forms/ChecksRadios.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/range',
              name: 'Range',
              component: () => import('@/views/forms/Range.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/input-group',
              name: 'Input Group',
              component: () => import('@/views/forms/InputGroup.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/floating-labels',
              name: 'Floating Labels',
              component: () => import('@/views/forms/FloatingLabels.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/layout',
              name: 'Layout',
              component: () => import('@/views/forms/Layout.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/forms/validation',
              name: 'Validation',
              component: () => import('@/views/forms/Validation.vue'),
              beforeEnter: ifAuthenticated,
            },
          ],
        },
        {
          path: '/charts',
          name: 'Charts',
          component: () => import('@/views/charts/Charts.vue'),
          beforeEnter: ifAuthenticated,
        },
        {
          path: '/icons',
          name: 'Icons',
          component: {
            render() {
              return h(resolveComponent('router-view'))
            },
          },
          beforeEnter: ifAuthenticated,
          redirect: '/icons/coreui-icons',
          children: [
            {
              path: '/icons/coreui-icons',
              name: 'CoreUI Icons',
              component: () => import('@/views/icons/CoreUIIcons.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/icons/brands',
              name: 'Brands',
              component: () => import('@/views/icons/Brands.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/icons/flags',
              name: 'Flags',
              component: () => import('@/views/icons/Flags.vue'),
              beforeEnter: ifAuthenticated,
            },
          ],
        },
        {
          path: '/notifications',
          name: 'Notifications',
          component: {
            render() {
              return h(resolveComponent('router-view'))
            },
          },
          beforeEnter: ifAuthenticated,
          redirect: '/notifications/alerts',
          children: [
            {
              path: '/notifications/alerts',
              name: 'Alerts',
              component: () => import('@/views/notifications/Alerts.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/notifications/badges',
              name: 'Badges',
              component: () => import('@/views/notifications/Badges.vue'),
              beforeEnter: ifAuthenticated,
            },
            {
              path: '/notifications/modals',
              name: 'Modals',
              component: () => import('@/views/notifications/Modals.vue'),
              beforeEnter: ifAuthenticated,
            },
          ],
        },
        {
          path: '/widgets',
          name: 'Widgets',
          component: () => import('@/views/widgets/Widgets.vue'),
          beforeEnter: ifAuthenticated,
        },
      ],
    },
    {
      path: '/pages',
      redirect: '/pages/404',
      name: 'Pages',
      component: {
        render() {
          return h(resolveComponent('router-view'))
        },
      },
      beforeEnter: ifNotAuthenticated,
      children: [
        {
          path: '404',
          name: 'Page404',
          component: () => import('@/views/pages/Page404'),
          beforeEnter: ifNotAuthenticated,
        },
        {
          path: '500',
          name: 'Page500',
          component: () => import('@/views/pages/Page500'),
          beforeEnter: ifNotAuthenticated,
        },
        {
          path: 'login',
          name: 'Login',
          component: () => import('@/views/pages/Login'),
          beforeEnter: ifNotAuthenticated,
        },
        {
          path: 'register',
          name: 'Register',
          component: () => import('@/views/pages/Register'),
          beforeEnter: ifNotAuthenticated,
        },
      ],
    },
  ]
  
  const router = createRouter({
    mode: 'history',
    history: createWebHistory(process.env.BASE_URL),
    routes,
    scrollBehavior() {
      // always scroll to top
      return { top: 0 }
    },
  })
  
  export default router
  