import router from "@/router";
import { routeData } from "@/router/constants/routes";
import { consumeReferralQuerystrings } from "@/router/methods/helpers/consume-referral-info";
import { initializeFromQueryStrings } from "@/router/methods/helpers/initialize-from-querystrings";
import { stashAllQueries } from "@/router/methods/helpers/querystring-stash";
import { queryStrings } from "@/constants/query-strings";
import store from "@/store";

export async function landingBeforeEnter(to, from) {
    await initializeFromQueryStrings();
    // Save querystrings to temporary stash so they can be referred to later.
    stashAllQueries(to);
    // Change referral info. For use in local testing for insurance crossover.
    await consumeReferralQuerystrings();

    // Check for fmgpage parameter
    const fmgPage = to?.query?.fmgPage;
    if (fmgPage && router.hasRoute(fmgPage)) {
        return {
            name: fmgPage,
            replace: true,
        };
    }

    const fromHeritage = to.query[queryStrings.FROM_HERITAGE];

    // If a session is still in memory and not loading a save quote, go to return-user.
    if (store.getters.vehicle?.year > 0 && !fromHeritage) {
        return {
            name: routeData.RETURN_USER.name,
            replace: true,
        };
    }

    // Otherwise, load session and auto-route
    return {
        name: routeData.LOAD_SESSION.name,
        replace: true,
    };
}
