// (C) 2021-2026 GoodData Corporation
import { batchActions } from "redux-batched-actions";
import { call, put, select } from "redux-saga/effects";
import { insightWidgetDrillsModified, } from "../../events/insight.js";
import { insightsActions } from "../../store/insights/index.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, getDrillDefinitionFromArray, } from "./validation/insightDrillDefinitionUtils.js";
import { getValidationData, validateDrillDefinition } from "./validation/insightDrillDefinitionValidation.js";
import { validateExistingInsightWidget } from "./validation/widgetValidations.js";
export function* modifyDrillsForInsightWidgetHandler(ctx, cmd) {
    const { payload: { drills: drillsToModify = [], blacklistHierarchiesToUpdate = [] }, correlationId, } = cmd;
    const widgets = yield select(selectWidgetsMap);
    const insightWidget = validateExistingInsightWidget(widgets, cmd, ctx);
    const { ref: widgetRef } = insightWidget;
    const validationData = yield call(getValidationData, widgetRef, drillsToModify, ctx);
    if (blacklistHierarchiesToUpdate.length > 0) {
        const blacklist = insightWidget.ignoredDrillDownHierarchies || [];
        yield put(tabsActions.replaceWidgetBlacklistHierarchies({
            ref: widgetRef,
            blacklistHierarchies: [...blacklist, ...blacklistHierarchiesToUpdate],
            undo: {
                cmd,
            },
        }));
    }
    const { drills: currentInsightDrills = [] } = insightWidget;
    const validatedDrillDefinition = drillsToModify.map((drillItem) => validateDrillDefinition(drillItem, validationData, ctx, cmd));
    const addedDrillDefinition = validatedDrillDefinition.filter((drillItem) => !existsDrillDefinitionInArray(drillItem, currentInsightDrills));
    const updatedDrillDefinition = validatedDrillDefinition.filter((drillItem) => existsDrillDefinitionInArray(drillItem, currentInsightDrills));
    // we need keep order of currentInsightDrills
    const drills = currentInsightDrills.map((drillItem) => {
        const updated = getDrillDefinitionFromArray(drillItem, updatedDrillDefinition);
        if (updated) {
            return updated;
        }
        return drillItem;
    });
    const updatedInsightDrills = [...drills, ...addedDrillDefinition];
    yield put(batchActions([
        insightsActions.addInsights(validationData.resolvedInsights.loaded),
        tabsActions.replaceWidgetDrills({
            ref: insightWidget.ref,
            drillDefinitions: updatedInsightDrills,
            undo: {
                cmd,
            },
        }),
    ]));
    // need to select the update widget to validate it by its new value
    const updatedWidget = yield select(selectWidgetByRef(insightWidget.ref));
    yield call(validateDrills, ctx, cmd, [updatedWidget]);
    yield call(validateDrillToCustomUrlParams, [updatedWidget]);
    return insightWidgetDrillsModified(ctx, widgetRef, addedDrillDefinition, updatedDrillDefinition, correlationId);
}
//# sourceMappingURL=modifyDrillsForInsightWidgetHandler.js.map