// (C) 2024-2026 GoodData Corporation
import { call, put, select } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { attributeFilterParentChanged } 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* setAttributeFilterDependentDateFiltersHandler(ctx, cmd) {
    const { filterLocalId, dependentDateFilters } = cmd.payload;
    const affectedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with localId ${filterLocalId} was not found.`);
    }
    yield put(tabsActions.setAttributeFilterDependentDateFilters({
        filterLocalId,
        dependentDateFilters,
    }));
    const changedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    invariant(changedFilter, "Inconsistent state in attributeFilterSetParentCommandHandler");
    yield dispatchDashboardEvent(attributeFilterParentChanged(ctx, changedFilter, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
