// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { drillTargetsAdded } from "../../events/drillTargets.js";
import { drillTargetsActions } from "../../store/drillTargets/index.js";
import { selectIsInEditMode } from "../../store/renderMode/renderModeSelectors.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { validateDrills } from "../common/validateDrills.js";
import { validateDrillToCustomUrlParams } from "../common/validateDrillToCustomUrlParams.js";
import { validateExistingInsightWidget } from "../widgets/validation/widgetValidations.js";
export function* addDrillTargetsHandler(ctx, cmd) {
    const { payload: { availableDrillTargets }, correlationId, } = cmd;
    const widgets = yield select(selectWidgetsMap);
    const insightWidget = validateExistingInsightWidget(widgets, cmd, ctx);
    const { ref, uri, identifier } = insightWidget;
    yield put(drillTargetsActions.addDrillTargets({
        identifier,
        uri,
        ref,
        availableDrillTargets,
    }));
    // in edit mode, we need to remove invalid drills in case the insight in the widget changes its drill targets
    // and also validate drill to custom URL parameters
    const isInEditMode = yield select(selectIsInEditMode);
    if (isInEditMode) {
        // Preserve the message from filter ref sanitization
        yield call(validateDrills, ctx, cmd, [insightWidget], {
            preserveSanitizedWarnings: true,
        });
        yield call(validateDrillToCustomUrlParams, [insightWidget]);
    }
    return drillTargetsAdded(ctx, ref, availableDrillTargets, correlationId);
}
//# sourceMappingURL=addDrillTargetsHandler.js.map