// (C) 2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { measureValueFilterConditionChanged } from "../../../events/filters.js";
import { invalidArgumentsProvided } from "../../../events/general.js";
import { dispatchDashboardEvent } from "../../../store/_infra/eventDispatcher.js";
import { selectFilterContextMeasureValueFilterByLocalId, selectWorkingFilterContextMeasureValueFilterByLocalId, } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../common.js";
export function* changeMeasureValueFilterConditionHandler(ctx, cmd) {
    const { localIdentifier, conditions, isWorkingSelectionChange } = cmd.payload;
    const existingFilter = yield select(selectFilterContextMeasureValueFilterByLocalId(localIdentifier));
    if (!existingFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Attempting to change condition of measure value filter ${localIdentifier} that does not exist on the dashboard.`);
    }
    yield put(tabsActions.changeMeasureValueFilterCondition({
        localIdentifier,
        conditions,
        ...(isWorkingSelectionChange === undefined ? {} : { isWorkingSelectionChange }),
    }));
    const updatedFilterSelector = isWorkingSelectionChange
        ? selectWorkingFilterContextMeasureValueFilterByLocalId(localIdentifier)
        : selectFilterContextMeasureValueFilterByLocalId(localIdentifier);
    const updatedFilter = yield select(updatedFilterSelector);
    invariant(updatedFilter, "Inconsistent state in changeMeasureValueFilterConditionHandler");
    yield dispatchDashboardEvent(measureValueFilterConditionChanged(ctx, localIdentifier, updatedFilter, conditions, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=changeMeasureValueFilterConditionHandler.js.map