// (C) 2021-2026 GoodData Corporation
import { put, select } from "redux-saga/effects";
import { kpiWidgetComparisonChanged } from "../../events/kpi.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { validateExistingKpiWidget } from "./validation/widgetValidations.js";
export function* changeKpiWidgetComparisonHandler(ctx, cmd) {
    const { payload: { comparison: { comparisonType, comparisonDirection }, }, correlationId, } = cmd;
    const widgets = yield select(selectWidgetsMap);
    const kpiWidget = validateExistingKpiWidget(widgets, cmd, ctx);
    const resolvedComparisonType = comparisonType ?? "none";
    const resolvedComparisonDirection = resolvedComparisonType === "none" ? undefined : (comparisonDirection ?? "growIsGood");
    yield put(tabsActions.replaceKpiWidgetComparison({
        ref: kpiWidget.ref,
        comparisonType: resolvedComparisonType,
        comparisonDirection: resolvedComparisonDirection,
        undo: {
            cmd,
        },
    }));
    const updatedWidgets = yield select(selectWidgetsMap);
    const updatedKpiWidget = updatedWidgets.get(kpiWidget.ref);
    return kpiWidgetComparisonChanged(ctx, kpiWidget.ref, updatedKpiWidget.kpi, correlationId);
}
