// (C) 2022-2026 GoodData Corporation
import { batchActions } from "redux-batched-actions";
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { attributeDisplayFormChanged } from "../../../events/filters.js";
import { invalidArgumentsProvided } from "../../../events/general.js";
import { dispatchDashboardEvent } from "../../../store/_infra/eventDispatcher.js";
import { selectEnableImmediateAttributeFilterDisplayAsLabelMigration, selectIsApplyFiltersAllAtOnceEnabledAndSet, } from "../../../store/config/configSelectors.js";
import { selectFilterContextAttributeFilterItemByLocalId } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../common.js";
import { resolveAndRegisterDisplayFormMetadata } from "./resolveDisplayFormMetadata.js";
import { validateFilterDisplayForm } from "./validation/filterDisplayFormValidation.js";
export function* changeAttributeDisplayFormHandler(ctx, cmd) {
    const { filterLocalId, displayForm, isWorkingSelectionChange, isResultOfMigration } = cmd.payload;
    const displayFormData = yield call(resolveAndRegisterDisplayFormMetadata, displayForm);
    invariant(displayFormData, "Inconsistent state in changeAttributeDisplayFormHandler, cannot update attribute filter with display form not available in the catalog.");
    const attribute = displayFormData?.attribute;
    const validationResult = yield call(validateFilterDisplayForm, ctx, attribute, displayForm);
    if (validationResult !== "VALID") {
        const message = validationResult === "INVALID_ATTRIBUTE_DISPLAY_FORM"
            ? "The display form provided is not a valid display form of the attribute filter."
            : "Cannot find the attribute for the display form used on the attribute filter.";
        throw invalidArgumentsProvided(ctx, cmd, message);
    }
    const enableImmediateAttributeFilterDisplayAsLabelMigration = yield select(selectEnableImmediateAttributeFilterDisplayAsLabelMigration);
    const isApplyAllAtOnceEnabledAndSet = yield select(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    yield put(batchActions([
        tabsActions.changeAttributeDisplayForm({
            filterLocalId,
            displayForm,
            isWorkingSelectionChange: isWorkingSelectionChange &&
                !enableImmediateAttributeFilterDisplayAsLabelMigration &&
                isApplyAllAtOnceEnabledAndSet,
            enableImmediateAttributeFilterDisplayAsLabelMigration,
            isResultOfMigration,
        }),
    ]));
    const changedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    invariant(changedFilter, "Inconsistent state in changeAttributeDisplayFormHandler, cannot update attribute filter for given local identifier.");
    yield dispatchDashboardEvent(attributeDisplayFormChanged(ctx, changedFilter, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=changeAttributeDisplayFormHandler.js.map