import { queryStrings } from "@/constants/query-strings";
import { getQuerystringParameter } from "@/helpers/querystring-helper";
import { saveSession } from "@/helpers/heritage-integration/order-helper.js";
import { storeActions } from "@/constants/store-actions.js";
import experimentMixin from "@/mixins/experiment-mixin";
import { experimentSettings } from "@/constants/experiments";
import { includesWindshieldReplacement } from "@/helpers/damage-helper";
import { applicationConfig } from "@/constants/application-config.js";
import store from "@/store";
import router from "@/router";
import { debugLog } from "@/helpers/debug-log-helper";

/*
    Used to navigate to the heritage funnel with the correct query string and url.
*/
export async function navigateToHeritageFunnel({ shouldSaveSession, pageNameToLog, navType }) {
    debugLog("--- Navigate to heritage start ---");
    debugLog("applicationConfig:", applicationConfig);
    debugLog("navigateToHeritageFunnel:", applicationConfig.HERITAGE_FUNNEL);
    debugLog("--- Navigate to heritage end ---");

    // Create the order (or save existing order) when navigating to Heritage Funnel.
    if (shouldSaveSession) {
        await saveSession({ pageNameToLog: pageNameToLog, shouldAwaitSaveSessionQueue: true });
    }

    var heritageParms = {
        corid: store.getters.order.referralCorrelationId,
        src: "concept-funnel",
        conceptsqid: store.getters.applicationUser.savedSessionId,
        isInsurance: store.getters.payment.isInsurance,
    };

    const promo = getQuerystringParameter(queryStrings.PROMO);

    if (promo) {
        heritageParms["promo"] = promo;
    }

    if (navType) {
        heritageParms["navType"] = navType;
    }

    if (
        (applicationConfig.HERITAGE_FUNNEL.includes("fixmyglasstest") &&
            applicationConfig.CURRENT_ENVIRONMENT === "Localhost") ||
        (applicationConfig.HERITAGE_FUNNEL.includes("fixmyglassdev") &&
            applicationConfig.CURRENT_ENVIRONMENT === "Localhost") ||
        (applicationConfig.HERITAGE_FUNNEL.includes("localhost") &&
            applicationConfig.CURRENT_ENVIRONMENT === "Localhost")
    ) {
        heritageParms["forceEndToEndInsurance"] = true;
    }

    // clear the heritage session that may still exist after an order was already submitted
    if (window.sessionStorage.getItem("createNewSessionForHeritage") !== null) {
        heritageParms["cns"] = "all";
        window.sessionStorage.removeItem("createNewSessionForHeritage");
    }

    router.navigateToExternalUrl(applicationConfig.HERITAGE_FUNNEL, heritageParms);
}

export async function skipVinLookup() {
    if (store.getters.damage.isRepair) {
        return true;
    }
    const isVinOptionalVehicle = store.getters.order.vehicle.make
        ? await store.dispatch(storeActions.IS_VIN_OPTIONAL_VEHICLE)
        : false;

    return (
        isVinOptionalVehicle ||
        !includesWindshieldReplacement() ||
        experimentMixin.methods.hasSettingEqualTo(experimentSettings.SUPPRESS_VIN_CAPTURE, "true")
    );
}

export async function skipVinLookupNotRepair() {
    if (store.getters.damage.isRepair) {
        return false;
    }

    const isVinOptionalVehicle = store.getters.order.vehicle.make
        ? await store.dispatch(storeActions.IS_VIN_OPTIONAL_VEHICLE)
        : false;

    return (
        isVinOptionalVehicle ||
        !includesWindshieldReplacement() ||
        experimentMixin.methods.hasSettingEqualTo(experimentSettings.SUPPRESS_VIN_CAPTURE, "true")
    );
}
