import { routes } from "@/router/methods/routes";
import { afterEach } from "@/router/methods/after-each";
import { beforeEach } from "@/router/methods/before-each";
import { handleSoftError, handleHardError } from "@/router/methods/error";
import {
    navigateWithoutSaving,
    navigateWithSaving,
    navigateWithPageData,
    navigateAndForceTopLevelNavigation,
} from "@/router/methods/navigate";
import { navigateToExternalUrl } from "@/router/methods/navigate-external";
import { sendToReturnUser } from "@/router/methods/send-to-return-user";
import { createWebHistory, createRouter } from "vue-router";
import { ROUTE_PREFIX_FULL } from "@/router/constants/route-prefix";

const router = createRouter({
    history: createWebHistory(ROUTE_PREFIX_FULL),
    routes,
    // Scroll to top on navigation.
    scrollBehavior(to, from, savedPosition) {
        return { top: 0 };
    },
});

// Set guards
router.afterEach(async (to, from) => afterEach(to, from));
router.beforeEach(async (to, from) => beforeEach(to, from));

// Bind methods for external use
router.navigateWithSaving = navigateWithSaving;
router.navigateWithoutSaving = navigateWithoutSaving;
router.navigateWithPageData = navigateWithPageData;
router.navigateAndForceTopLevelNavigation = navigateAndForceTopLevelNavigation;

router.handleSoftError = handleSoftError;
router.handleHardError = handleHardError;

router.navigateToExternalUrl = navigateToExternalUrl;

router.sendToReturnUser = sendToReturnUser;

export default router;
