import { queryStrings } from "@/constants/query-strings";
import { consumeReferralQuerystrings } from "@/router/methods/helpers/consume-referral-info";
import { loadSessionIfPresent } from "@/helpers/heritage-integration/order-helper";
import { routeData } from "@/router/constants/routes";
import { getFunnelCookie } from "@/helpers/heritage-integration/cookie-helper";

export async function handleHeritageReturn(to, from) {
    const fromHeritageFlag = to.query[queryStrings.FROM_HERITAGE];
    const didHeritageFunnelUpdateLast = getFunnelCookie()?.DidHeritageFunnelUpdateLast;

    if (fromHeritageFlag || didHeritageFunnelUpdateLast) {
        // Get new referral info from querystring in case passed there.
        await consumeReferralQuerystrings();

        // load session with new cookie
        await loadSessionIfPresent(true, routeData.SERVICE_LOCATION.name);
    }
}
