// (C) 2021-2025 GoodData Corporation
import { all, call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { attributeFilterSelectionChanged } 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 { selectIsCrossFiltering, selectIsFilterFromCrossFilteringByLocalIdentifier, } from "../../../store/drill/drillSelectors.js";
import { selectAttributeFilterDescendants, selectFilterContextAttributeFilterByLocalId, } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { dispatchFilterContextChanged, resetCrossFiltering } from "../common.js";
export function* changeAttributeFilterSelectionHandler(ctx, cmd) {
    const { elements, filterLocalId, selectionType, isWorkingSelectionChange, isResultOfMigration, isSelectionInvalid, } = cmd.payload;
    // validate filterLocalId
    const affectedFilter = yield select(selectFilterContextAttributeFilterByLocalId(cmd.payload.filterLocalId));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with filterLocalId ${filterLocalId} not found.`);
    }
    const isApplyAllAtOnceEnabledAndSet = yield select(selectIsApplyFiltersAllAtOnceEnabledAndSet);
    const enableImmediateAttributeFilterDisplayAsLabelMigration = yield select(selectEnableImmediateAttributeFilterDisplayAsLabelMigration);
    yield put(tabsActions.updateAttributeFilterSelection({
        elements,
        filterLocalId,
        negativeSelection: selectionType === "NOT_IN",
        isWorkingSelectionChange: isWorkingSelectionChange && isApplyAllAtOnceEnabledAndSet,
        enableImmediateAttributeFilterDisplayAsLabelMigration,
        isResultOfMigration,
        isSelectionInvalid,
    }));
    const changedFilter = yield select(selectFilterContextAttributeFilterByLocalId(cmd.payload.filterLocalId));
    invariant(changedFilter, "Inconsistent state in attributeFilterChangeSelectionCommandHandler");
    if (!ctx.backend.capabilities.supportsKeepingDependentFiltersSelection) {
        const childFiltersIds = yield select(selectAttributeFilterDescendants(changedFilter.attributeFilter.localIdentifier));
        yield all(childFiltersIds.map((childFilterId) => put(tabsActions.updateAttributeFilterSelection({
            filterLocalId: childFilterId,
            elements: {
                uris: [],
            },
            negativeSelection: true,
            isWorkingSelectionChange: isWorkingSelectionChange && isApplyAllAtOnceEnabledAndSet,
        }))));
    }
    const isCrossFiltering = yield select(selectIsCrossFiltering);
    const isCurrentFilterCrossFiltering = yield select(selectIsFilterFromCrossFilteringByLocalIdentifier(filterLocalId));
    if (isCrossFiltering &&
        !isCurrentFilterCrossFiltering &&
        (!isWorkingSelectionChange || !isApplyAllAtOnceEnabledAndSet)) {
        yield call(resetCrossFiltering, cmd);
    }
    yield dispatchDashboardEvent(attributeFilterSelectionChanged(ctx, changedFilter, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=changeAttributeFilterSelectionHandler.js.map