// (C) 2021-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { isDashboardAttributeFilter, } from "@gooddata/sdk-model";
import { kpiWidgetFilterSettingsChanged, } from "../../events/kpi.js";
import { tabsActions } from "../../store/tabs/index.js";
import { selectWidgetsMap } from "../../store/tabs/layout/layoutSelectors.js";
import { processFilterOp } from "./common/filterOperations.js";
import { validateAttributeFiltersToIgnore, validateDatasetForKpiWidgetDateFilter, } from "./validation/filterValidation.js";
import { validateExistingKpiWidget } from "./validation/widgetValidations.js";
const KpiWidgetFilterValidations = {
    dateDatasetValidator: validateDatasetForKpiWidgetDateFilter,
    attributeFilterValidator: validateAttributeFiltersToIgnore,
};
/**
 * 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* changeKpiWidgetFilterSettingsHandler(ctx, cmd) {
    const widgets = yield select(selectWidgetsMap);
    const kpiWidget = validateExistingKpiWidget(widgets, cmd, ctx);
    const result = yield call(processFilterOp, ctx, KpiWidgetFilterValidations, cmd, kpiWidget);
    const { dateDataSet, ignoredFilters } = result;
    // KPI supports only attribute filters
    const ignoredAttributeFilters = ignoredFilters?.filter(isDashboardAttributeFilter);
    const ignoreDashboardFilters = ignoredAttributeFilters?.map((filter) => {
        const filterReference = {
            type: "attributeFilterReference",
            displayForm: filter.attributeFilter.displayForm,
        };
        return filterReference;
    });
    yield put(tabsActions.replaceWidgetFilterSettings({
        ref: kpiWidget.ref,
        dateDataSet: dateDataSet?.dataSet.ref,
        ignoreDashboardFilters,
        undo: {
            cmd,
        },
    }));
    return kpiWidgetFilterSettingsChanged(ctx, kpiWidget.ref, ignoredAttributeFilters ?? [], result.dateDataSet, cmd.correlationId);
}
