// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { insightWidgetDrillsRemoved, } from "../../events/insight.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetByRef, selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { validateDrills } from "../common/validateDrills.js";
import { validateDrillToCustomUrlParams } from "../common/validateDrillToCustomUrlParams.js";
import { existsDrillDefinitionInArray } from "./validation/insightDrillDefinitionUtils.js";
import { validateRemoveDrillsByLocalIdentifier } from "./validation/removeDrillsSelectorValidation.js";
import { validateExistingInsightWidget } from "./validation/widgetValidations.js";
export function* removeDrillsForInsightWidgetHandler(ctx, cmd) {
    const { payload: { localIdentifiers }, correlationId, } = cmd;
    const widgets = yield select(selectWidgetsMap);
    const insightWidget = validateExistingInsightWidget(widgets, cmd, ctx);
    const { ref: widgetRef, drills: currentInsightDrills } = insightWidget;
    const drillsToRemove = validateRemoveDrillsByLocalIdentifier(localIdentifiers, currentInsightDrills, ctx, cmd);
    const notModifiedDrillDefinition = currentInsightDrills.filter((drillItem) => !existsDrillDefinitionInArray(drillItem, drillsToRemove));
    yield put(tabsActions.replaceWidgetDrills({
        ref: widgetRef,
        drillDefinitions: notModifiedDrillDefinition,
        undo: {
            cmd,
        },
    }));
    // need to select the update widget to validate it by its new value
    const updatedWidget = yield select(selectWidgetByRef(widgetRef));
    yield call(validateDrills, ctx, cmd, [updatedWidget]);
    yield call(validateDrillToCustomUrlParams, [updatedWidget]);
    return insightWidgetDrillsRemoved(ctx, widgetRef, drillsToRemove, correlationId);
}
