// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { kpiWidgetDrillSet } from "../../events/kpi.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { validateKpiDrill } from "./validation/kpiDrillValidation.js";
import { validateExistingKpiWidget } from "./validation/widgetValidations.js";
export function* setDrillForKpiWidgetHandler(ctx, cmd) {
    const { correlationId } = cmd;
    const { legacyDashboardTabIdentifier, legacyDashboardRef } = cmd.payload;
    const widgets = yield select(selectWidgetsMap);
    const kpiWidget = validateExistingKpiWidget(widgets, cmd, ctx);
    const { ref: widgetRef } = kpiWidget;
    const drill = {
        tab: legacyDashboardTabIdentifier,
        target: legacyDashboardRef,
        origin: {
            type: "drillFromMeasure",
            measure: kpiWidget.kpi.metric,
        },
        type: "drillToLegacyDashboard",
        transition: "in-place",
    };
    yield call(validateKpiDrill, drill, ctx, cmd);
    yield put(tabsActions.replaceKpiWidgetDrill({
        ref: widgetRef,
        drill,
        undo: {
            cmd,
        },
    }));
    return kpiWidgetDrillSet(ctx, widgetRef, drill, correlationId);
}
