import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import { accountsDB } from "@/db";

/**
 *
 * @param to :RouteLocationNormalized
 * @param from :RouteLocationNormalized
 * @param next :NavigationGuardNext
 */
const enterOrStart = async (to, from, next) => {
  await accountsDB.open();
  const num_accounts = await accountsDB.accounts.count();
  if (num_accounts > 0) {
    next();
  } else {
    next({ name: "start" });
  }
};

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "home",
    component: () =>
      import(/* webpackChunkName: "home" */ "../views/HomeView.vue"),
    beforeEnter: enterOrStart,
  },
  {
    path: "/account",
    name: "account",
    component: () =>
      import(/* webpackChunkName: "account" */ "../views/AccountViewView.vue"),
    beforeEnter: enterOrStart,
  },
  {
    path: "/confirm-contact",
    name: "confirm-contact",
    component: () =>
      import(
        /* webpackChunkName: "confirm-contact" */ "../views/ConfirmContactView.vue"
      ),
  },
  {
    path: "/contact-amounts",
    name: "contact-amounts",
    component: () =>
      import(
        /* webpackChunkName: "contact-amounts" */ "../views/ContactAmountsView.vue"
      ),
  },
  {
    path: "/contact-qr",
    name: "contact-qr",
    component: () =>
      import(
        /* webpackChunkName: "contact-qr" */ "../views/ContactQRScanShowView.vue"
      ),
  },
  {
    path: "/contacts",
    name: "contacts",
    component: () =>
      import(/* webpackChunkName: "contacts" */ "../views/ContactsView.vue"),
    beforeEnter: enterOrStart,
  },
  {
    path: "/scan-contact",
    name: "scan-contact",
    component: () =>
      import(
        /* webpackChunkName: "scan-contact" */ "../views/ContactScanView.vue"
      ),
  },
  {
    path: "/discover",
    name: "discover",
    component: () =>
      import(/* webpackChunkName: "discover" */ "../views/DiscoverView.vue"),
  },
  {
    path: "/help",
    name: "help",
    component: () =>
      import(/* webpackChunkName: "help" */ "../views/HelpView.vue"),
  },
  {
    path: "/import-account",
    name: "import-account",
    component: () =>
      import(
        /* webpackChunkName: "import-account" */ "../views/ImportAccountView.vue"
      ),
  },
  {
    path: "/new-edit-account",
    name: "new-edit-account",
    component: () =>
      import(
        /* webpackChunkName: "new-edit-account" */ "../views/NewEditAccountView.vue"
      ),
  },
  {
    path: "/new-edit-commitment",
    name: "new-edit-commitment",
    component: () =>
      import(
        /* webpackChunkName: "new-edit-commitment" */ "../views/NewEditCommitmentView.vue"
      ),
  },
  {
    path: "/new-edit-project",
    name: "new-edit-project",
    component: () =>
      import(
        /* webpackChunkName: "new-edit-project" */ "../views/NewEditProjectView.vue"
      ),
  },
  {
    path: "/new-identifier",
    name: "new-identifier",
    component: () =>
      import(
        /* webpackChunkName: "new-identifier" */ "../views/NewIdentifierView.vue"
      ),
  },
  {
    path: "/identity-switcher",
    name: "identity-switcher",
    component: () =>
      import(
        /* webpackChunkName: "identity-switcher" */ "../views/IdentitySwitcherView.vue"
      ),
  },
  {
    path: "/project",
    name: "project",
    component: () =>
      import(/* webpackChunkName: "project" */ "../views/ProjectViewView.vue"),
  },
  {
    path: "/projects",
    name: "projects",
    component: () =>
      import(/* webpackChunkName: "projects" */ "../views/ProjectsView.vue"),
    beforeEnter: enterOrStart,
  },
  {
    path: "/seed-backup",
    name: "seed-backup",
    component: () =>
      import(
        /* webpackChunkName: "seed-backup" */ "../views/SeedBackupView.vue"
      ),
  },
  {
    path: "/start",
    name: "start",
    component: () =>
      import(/* webpackChunkName: "start" */ "../views/StartView.vue"),
  },
  {
    path: "/statistics",
    name: "statistics",
    component: () =>
      import(
        /* webpackChunkName: "statistics" */ "../views/StatisticsView.vue"
      ),
  },
];

/** @type {*} */
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;
