import analyticsMixin from "@/mixins/analytics-mixin";
import { routeData } from "@/router/constants/routes";
import router from "@/router";
import store from "@/store";
import { storeActions } from "@/constants/store-actions";
import { deleteFunnelCookie } from "@/helpers/heritage-integration/cookie-helper";
import experimentMixin from "../../mixins/experiment-mixin";

export async function handleSoftError(errorPayload, forceRestart = false) {
    if (forceRestart) {
        await store.dispatch(storeActions.RESET_STATE);
        deleteFunnelCookie();
    }

    analyticsMixin.methods.pushPageErrorToDataLayer(errorPayload);

    router.push({
        name: routeData.ERROR.name,
    });
}

export async function handleHardError(errorPayload) {
    try {
        const isInStaticErrorExperiment = experimentMixin.methods.hasSettingEqualTo(
            "UseStaticErrorPage",
            "true"
        );

        if (isInStaticErrorExperiment) {
            analyticsMixin?.methods?.pushPageErrorToDataLayer(errorPayload);
            window.top.location = "/fmg/static/error";
            return;
        } else {
            await handleSoftError(errorPayload, true);
            return;
        }
    } catch (exception) {
        await handleSoftError(errorPayload, true);
    }
}
