// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { dashboardAttributeFilterItemDisplayForm, isDashboardAttributeFilter, isDashboardAttributeFilterItem, isDashboardDateFilterWithDimension, } from "@gooddata/sdk-model";
import { insightWidgetFilterSettingsChanged, } from "../../events/insight.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { processFilterOp } from "./common/filterOperations.js";
import { validateAttributeFiltersToIgnore, validateDatasetForInsightWidgetDateFilter, validateDateFiltersToIgnore, } from "./validation/filterValidation.js";
import { validateExistingInsightWidget } from "./validation/widgetValidations.js";
const InsightWidgetFilterValidations = {
    dateDatasetValidator: validateDatasetForInsightWidgetDateFilter,
    attributeFilterValidator: validateAttributeFiltersToIgnore,
    dateFilterValidator: validateDateFiltersToIgnore,
};
/**
 * Filter setting handler contains some of the more complex validations.
 *
 * If command specifies date dataset to use for date filter (meaning at the same time that date filter should be enabled),
 * then the date dataset validation occurs. This needs to perform complex query processing first to obtain all available
 * date datasets for the insight widget.
 *
 * If command specifies refs of display forms to ignore attribute filters by, then another validation occurs. This one
 * will ensure that the display form refs on the input represent valid, existing display forms. And then ensure that
 * those display forms are actually used in currently used attribute filters.
 */
export function* changeInsightWidgetFilterSettingsHandler(ctx, cmd) {
    const widgets = yield select(selectWidgetsMap);
    const insightWidget = validateExistingInsightWidget(widgets, cmd, ctx);
    const result = yield call(processFilterOp, ctx, InsightWidgetFilterValidations, cmd, insightWidget);
    const { dateDataSet, ignoredFilters } = result;
    const ignoreDashboardFilters = ignoredFilters?.map((filter) => {
        if (isDashboardAttributeFilterItem(filter)) {
            const filterReference = {
                type: "attributeFilterReference",
                displayForm: dashboardAttributeFilterItemDisplayForm(filter),
            };
            return filterReference;
        }
        const filterReference = {
            type: "dateFilterReference",
            dataSet: filter.dateFilter.dataSet,
        };
        return filterReference;
    });
    yield put(tabsActions.replaceWidgetFilterSettings({
        ref: insightWidget.ref,
        dateDataSet: dateDataSet?.dataSet.ref,
        ignoreDashboardFilters,
        undo: {
            cmd,
        },
    }));
    return insightWidgetFilterSettingsChanged(ctx, insightWidget.ref, (ignoredFilters ?? []).filter(isDashboardAttributeFilter), result.dateDataSet, cmd.correlationId, (ignoredFilters ?? []).filter(isDashboardDateFilterWithDimension));
}
//# sourceMappingURL=changeInsightWidgetFilterSettingsHandler.js.map