// (C) 2023-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { attributeDisplayTitleChanged } 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 "../common.js";
export function* changeAttributeTitleHandler(ctx, cmd) {
    const { filterLocalId, title } = cmd.payload;
    // validate filterLocalId
    const affectedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(cmd.payload.filterLocalId));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with filterLocalId ${filterLocalId} not found.`);
    }
    yield put(tabsActions.changeAttributeTitle({
        filterLocalId,
        title,
    }));
    const changedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(cmd.payload.filterLocalId));
    invariant(changedFilter, "Inconsistent state in changeAttributeTitleHandler, cannot update attribute filter for given local identifier.");
    yield dispatchDashboardEvent(attributeDisplayTitleChanged(ctx, changedFilter, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=changeAttributeTitleHandler.js.map