import { lazyLoadComponent } from "@/router/methods/helpers/lazy-load-component";
import { routeData } from "@/router/constants/routes";

export function createRoute(routeDatum, beforeEnter = async (to, from) => undefined) {
    return {
        name: routeDatum.name,
        path: routeDatum.path,
        component: lazyLoadComponent(routeDatum.name),
        beforeEnter: async (to, from) => {
            try {
                return await beforeEnter(to, from);
            } catch {
                return {
                    name: routeData.ERROR.name,
                    replace: true,
                };
            }
        },
    };
}

export function createVirtualRoute(routeDatum, beforeEnter = async (to, from) => undefined) {
    return {
        name: routeDatum.name,
        path: routeDatum.path,
        beforeEnter: async (to, from) => {
            try {
                return await beforeEnter(to, from);
            } catch (error) {
                console.log(error);
                return {
                    name: routeData.ERROR.name,
                    replace: true,
                };
            }
        },
        virtual: true,
    };
}
