import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

// scrollBehavior:
// - only available in html5 history mode
// - defaults to no scroll behavior
// - return false to prevent scroll
const scrollBehavior = function(to, from, savedPosition) {
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition;
  } else {
    const position = {};

    // scroll to anchor by returning the selector
    if (to.hash) {
      position.selector = to.hash;

      // specify offset of the element
      if (to.hash === "#anchor2") {
        position.offset = { y: 100 };
      }

      if (document.querySelector(to.hash)) {
        return position;
      }

      // if the returned position is falsy or an empty object,
      // will retain current scroll position.

      return false;
    }

    // eslint-disable-next-line
    return new Promise(resolve => {
      // check if any matched route config has meta that requires scrolling to top
      // eslint-disable-next-line
      if (to.matched.some(m => m.meta.scrollToTop)) {
        // coords will be used if no selector is provided,
        // or if the selector didn't match any element.
        position.x = 0;
        position.y = 0;
      }

      // wait for the out transition to complete (if necessary)
      this.app.$root.$once("triggerScroll", () => {
        // if the resolved position is falsy or an empty object,
        // will retain current scroll position.

        resolve(position);
      });
    });
  }
};
/**
 * https://router.vuejs.org/guide/advanced/lazy-loading.html#grouping-components-in-the-same-chunk
 *
 *
 */
export default new Router({
  mode: "history",
  base: process.env.BASE_URL,
  scrollBehavior,
  routes: [
    //########################## GENERAL / COMMON GROUP ###################################
    {
      path: "/open/:socktype/:recordid",
      name: "sock-open",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/sock-open.vue")
    },
    {
      path: "/viewreport/:oid/:rid",
      name: "sock-report-view",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/sock-report-view.vue"
        )
    },
    {
      path: "/about",
      name: "sock-about",
      // 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: "sock-common" */ "./views/sock-about.vue")
    },
    {
      path: "/applog",
      name: "sock-log",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/sock-log.vue")
    },
    {
      path: "/customize/:formCustomTemplateKey",
      name: "sock-customize",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/sock-customize.vue"
        )
    },
    {
      path: "/data-list-column-view/:dataListKey",
      name: "sock-data-list-column-view",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/sock-data-list-column-view.vue"
        )
    },

    {
      path: "/history/:socktype/:recordid/:userlog?",
      name: "sock-history",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/sock-history.vue")
    },
    {
      path: "/login",
      name: "login",
      meta: { scrollToTop: true }, //KEEP THIS AS AN EXAMPLE OF HOW TO USE WITH CODE ABOVE
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/login.vue")
    },
    {
      path: "/home-dashboard",
      name: "home-dashboard",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-dashboard.vue"
        )
    },
    {
      //path: "/home-search/:socktype?", this was making it sticky and couldn't change type as it was in url path
      path: "/home-search",
      name: "home-search",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-search.vue")
    },
    { path: "/", redirect: "/login" }, //If someone goes blindly to the root of the app, then it should go to login
    {
      path: "/home-schedule",
      name: "home-schedule",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-schedule.vue"
        )
    },
    {
      path: "/home-memos",
      name: "home-memos",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-memos.vue")
    },
    {
      path: "/home-memos/:recordid",
      name: "memo-edit",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-memo.vue")
    },
    {
      path: "/home-notifications",
      name: "home-notifications",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-notifications.vue"
        )
    },
    {
      path: "/home-notify-direct",
      name: "home-notify-direct",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-notify-direct.vue"
        )
    },
    {
      path: "/home-reminders",
      name: "home-reminders",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-reminders.vue"
        )
    },
    {
      path: "/home-reminders/:recordid",
      name: "reminder-edit",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-reminder.vue"
        )
    },
    {
      path: "/home-reviews/:aType?/:objectId?/:name?",
      name: "home-reviews",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-reviews.vue")
    },
    {
      path: "/home-reviews/:recordid/:aType?/:objectId?/:name?",
      name: "review-edit",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-review.vue")
    },
    {
      path: "/home-user-settings",
      name: "home-user-settings",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-user-settings.vue"
        )
    },
    {
      path: "/home-reset",
      name: "home-reset",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/home-reset.vue")
    },
    {
      path: "/home-password",
      name: "home-password",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-password.vue"
        )
    },
    {
      path: "/home-security",
      name: "home-security",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-security.vue"
        )
    },
    {
      path: "/home-notify-subscriptions",
      name: "home-notify-subscriptions",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-notify-subscriptions.vue"
        )
    },
    {
      path: "/home-notify-subscriptions/:recordid",
      name: "home-notify-subscription",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/home-notify-subscription.vue"
        )
    },
    //####################### CUSTOMERS GROUP ##############################
    {
      path: "/cust-customers",
      name: "cust-customers",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-customers.vue")
    },
    {
      path: "/cust-customers/:recordid",
      name: "customer-edit",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-customer.vue")
    },
    {
      path: "/cust-customer-notes/:customerid",
      name: "customer-notes",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-customer-notes.vue")
    },
    {
      path: "/cust-customer-note/:recordid",
      name: "customer-note-edit",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-customer-note.vue")
    },
    {
      path: "/cust-users",
      name: "cust-users",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-users.vue")
    },
    {
      path: "/cust-users/:recordid",
      name: "cust-user",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-user.vue")
    },
    {
      path: "/cust-head-offices",
      name: "cust-head-offices",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-head-offices.vue")
    },
    {
      path: "/cust-head-offices/:recordid",
      name: "head-office-edit",
      component: () =>
        import(/* webpackChunkName: "cust" */ "./views/cust-head-office.vue")
    },
    {
      path: "/cust-notify-subscriptions",
      name: "cust-notify-subscriptions",
      component: () =>
        import(
          /* webpackChunkName: "cust" */ "./views/customer-notify-subscriptions.vue"
        )
    },
    {
      path: "/cust-notify-subscriptions/:recordid",
      name: "cust-notify-subscription",
      component: () =>
        import(
          /* webpackChunkName: "cust" */ "./views/customer-notify-subscription.vue"
        )
    },

    //####################### BUSINESS GROUP ##############################
    {
      path: "/biz-license-list",
      name: "biz-license-list",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-license-list.vue")
    },
    {
      path: "/biz-license-list/:recordid",
      name: "license-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-license.vue")
    },
    {
      path: "/biz-trial-request-list",
      name: "biz-trial-request-list",
      component: () =>
        import(
          /* webpackChunkName: "biz" */ "./views/biz-trial-request-list.vue"
        )
    },
    {
      path: "/biz-trial-request-list/:recordid",
      name: "trial-request-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-trial-request.vue")
    },
    {
      path: "/biz-subscription-server-list",
      name: "biz-subscription-server-list",
      component: () =>
        import(
          /* webpackChunkName: "biz" */ "./views/biz-subscription-server-list.vue"
        )
    },
    {
      path: "/biz-subscription-server-list/:recordid",
      name: "subscription-server-edit",
      component: () =>
        import(
          /* webpackChunkName: "biz" */ "./views/biz-subscription-server.vue"
        )
    },
    {
      path: "/biz-purchase-list",
      name: "biz-purchase-list",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-purchase-list.vue")
    },
    {
      path: "/biz-purchase-list/:recordid",
      name: "purchase-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-purchase.vue")
    },
    {
      path: "/biz-vendor-notification-list",
      name: "biz-vendor-notification-list",
      component: () =>
        import(
          /* webpackChunkName: "biz" */ "./views/biz-vendor-notification-list.vue"
        )
    },
    {
      path: "/biz-vendor-notification-list/:recordid",
      name: "vendor-notification-edit",
      component: () =>
        import(
          /* webpackChunkName: "biz" */ "./views/biz-vendor-notification.vue"
        )
    },
    {
      path: "/biz-product-list",
      name: "biz-product-list",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-product-list.vue")
    },
    {
      path: "/biz-product-list/:recordid",
      name: "product-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-product.vue")
    },
    {
      path: "/biz-vendor-list",
      name: "biz-vendor-list",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-vendor-list.vue")
    },
    {
      path: "/biz-vendor-list/:recordid",
      name: "vendor-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-vendor.vue")
    },
    {
      path: "/biz-gzcase-list",
      name: "biz-gzcase-list",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-gzcase-list.vue")
    },
    {
      path: "/biz-gzcase-list/:recordid",
      name: "gzcase-edit",
      component: () =>
        import(/* webpackChunkName: "biz" */ "./views/biz-gzcase.vue")
    },
    // //####################### SERVICE GROUP ##############################
    // {
    //   path: "/svc-schedule",
    //   name: "svc-schedule",
    //   component: () =>
    //     import(/* webpackChunkName: "svc" */ "./views/svc-schedule.vue")
    // },

    //######################### ADMINISTRATION GROUP #####################################
    {
      path: "/adm-global-settings",
      name: "adm-global-settings",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-global-settings.vue")
    },
    {
      path: "/adm-global-select-templates",
      name: "adm-global-select-templates",
      component: () =>
        import(
          /* webpackChunkName: "adm" */ "./views/adm-global-select-templates.vue"
        )
    },

    {
      path: "/adm-global-logo",
      name: "adm-global-logo",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-global-logo.vue")
    },

    {
      path: "/adm-users",
      name: "adm-users",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-users.vue")
    },
    {
      path: "/adm-users/:recordid",
      name: "adm-user",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-user.vue")
    },
    {
      path: "/adm-translations",
      name: "adm-translations",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-translations.vue")
    },
    {
      path: "/adm-translations/:recordid",
      name: "adm-translation",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-translation.vue")
    },

    {
      path: "/adm-report-templates",
      name: "adm-report-templates",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-report-templates.vue")
    },
    {
      path: "/report-edit/:recordid", //Route to edit a report template
      name: "sock-report-edit",
      component: () =>
        import(
          //it gets it's own chunk name because it's huge and rarely used
          /* webpackChunkName: "sock-report-edit" */ "./views/sock-report-edit.vue"
        )
    },
    {
      path: "/adm-attachments",
      name: "adm-attachments",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-attachments.vue")
    },

    {
      path: "/adm-history",
      name: "adm-history",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-history.vue")
    },
    {
      path: "/adm-import",
      name: "adm-import",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-import.vue")
    },
    {
      path: "/adm-integrations",
      name: "adm-integrations",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-integrations.vue")
    },
    {
      path: "/adm-integrations/:recordid",
      name: "adm-integration",
      component: () =>
        import(/* webpackChunkName: "adm" */ "./views/adm-integration.vue")
    },

    //########################## OPERATIONS GROUP ############################
    {
      path: "/ops-backup",
      name: "ops-backup",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-backup.vue")
    },

    {
      path: "/ops-server-state",
      name: "ops-server-state",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-server-state.vue")
    },

    {
      path: "/ops-jobs",
      name: "ops-jobs",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-jobs.vue")
    },

    {
      path: "/ops-log",
      name: "ops-log",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-log.vue")
    },
    {
      path: "/ops-view-configuration",
      name: "ops-view-configuration",
      component: () =>
        import(
          /* webpackChunkName: "ops" */ "./views/ops-view-configuration.vue"
        )
    },

    {
      path: "/ops-metrics",
      name: "ops-metrics",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-metrics.vue")
    },
    {
      path: "/ops-profile",
      name: "ops-profile",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-profile.vue")
    },
    {
      path: "/ops-notify-queue",
      name: "ops-notify-queue",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-notify-queue.vue")
    },
    {
      path: "/ops-notification-settings",
      name: "ops-notification-settings",
      component: () =>
        import(
          /* webpackChunkName: "ops" */ "./views/ops-notification-settings.vue"
        )
    },
    {
      path: "/ops-notify-log",
      name: "ops-notify-log",
      component: () =>
        import(/* webpackChunkName: "ops" */ "./views/ops-notify-log.vue")
    },
    {
      path: "/ops-customer-notify-log",
      name: "ops-customer-notify-log",
      component: () =>
        import(
          /* webpackChunkName: "ops" */ "./views/ops-customer-notify-log.vue"
        )
    },

    //##############################  SPECIAL ROUTES ###############################

    {
      //No rights - happens when customer user logs in without access to anything at all due to configuration not allowing it
      path: "/no-features-available",
      name: "no-features-available",
      component: () =>
        import(
          /* webpackChunkName: "sock-common" */ "./views/nofeaturesavailable.vue"
        )
    },
    {
      //404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404 404
      path: "*",
      name: "notfound",
      component: () =>
        import(/* webpackChunkName: "sock-common" */ "./views/notfound.vue")
    }
  ]
});
