// (C) 2023-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { dateFilterModeChanged } from "../../events/filters.js";
import { invalidArgumentsProvided } from "../../events/general.js";
import { dispatchDashboardEvent } from "../../store/_infra/eventDispatcher.js";
import { selectDateFilterConfigsOverrides } from "../../store/tabs/dateFilterConfigs/dateFilterConfigsSelectors.js";
import { selectFilterContextDateFilterByDataSet } from "../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../filterContext/common.js";
export function* changeDateFilterWithDimensionModeHandler(ctx, cmd) {
    const { dataSet } = cmd.payload;
    // validate dataSet
    const affectedFilter = yield select(selectFilterContextDateFilterByDataSet(dataSet));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with data set ${dataSet} not found.`);
    }
    yield put(tabsActions.changeDateFilterConfigsMode(cmd.payload));
    const changedFilter = yield select(selectFilterContextDateFilterByDataSet(dataSet));
    const filterConfigs = yield select(selectDateFilterConfigsOverrides);
    const changedFilterConfig = filterConfigs.find((item) => item.dateDataSet === dataSet);
    invariant(changedFilter, "Inconsistent state in changeDateModeHandler, cannot update date filter for given data set.");
    invariant(changedFilterConfig, "Inconsistent state in changeDateModeHandler, cannot update date filter config for given data set.");
    yield dispatchDashboardEvent(dateFilterModeChanged(ctx, changedFilter, changedFilterConfig.config));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=changeDateFilterWithDimensionModeHandler.js.map