// (C) 2024-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { dashboardAttributeConfigLimitingItemsChanged } from "../../events/filters.js";
import { invalidArgumentsProvided } from "../../events/general.js";
import { dispatchDashboardEvent } from "../../store/_infra/eventDispatcher.js";
import { selectFilterContextAttributeFilterItemByLocalId } from "../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../filterContext/common.js";
export function* changeAttributeFilterLimitingItemsHandler(ctx, cmd) {
    const filterLocalId = yield call(getValidatedFilterLocalIdentifier, ctx, cmd);
    yield put(tabsActions.changeLimitingItems({
        filterLocalId,
        limitingItems: cmd.payload.limitingItems,
    }));
    const changedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    invariant(changedFilter, "Inconsistent state in changeAttributeModeHandler, cannot update attribute filter for given local identifier.");
    yield dispatchDashboardEvent(dashboardAttributeConfigLimitingItemsChanged(ctx, changedFilter));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
function* getValidatedFilterLocalIdentifier(ctx, cmd) {
    const { localIdentifier } = cmd.payload;
    const affectedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(localIdentifier));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with localIdentifier ${localIdentifier} not found.`);
    }
    return localIdentifier;
}
//# sourceMappingURL=changeAttributeFilterLimitingItemsHandler.js.map