// (C) 2021-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 { selectAttributeFilterDisplayFormsMap, selectFilterContextAttributeFilterItemByLocalId, selectFilterContextAttributeFilterItems, } from "../../../store/tabs/filterContext/filterContextSelectors.js";
import { tabsActions } from "../../../store/tabs/index.js";
import { dispatchFilterContextChanged } from "../common.js";
import { validateAttributeFilterParents } from "./validation/parentFiltersValidation.js";
export function* setAttributeFilterParentsHandler(ctx, cmd) {
    const { filterLocalId, parentFilters } = cmd.payload;
    const allFilters = yield select(selectFilterContextAttributeFilterItems);
    const affectedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    const displayFormsMap = yield select(selectAttributeFilterDisplayFormsMap);
    if (!affectedFilter) {
        throw invalidArgumentsProvided(ctx, cmd, `Filter with localId ${filterLocalId} was not found.`);
    }
    const validationResult = yield call(validateAttributeFilterParents, ctx, affectedFilter, [...parentFilters], allFilters, displayFormsMap);
    if (validationResult !== "VALID") {
        const message = validationResult === "EXTRANEOUS_PARENT"
            ? "Some of the parents provided cannot be used because filters for those are not in the filters collection. " +
                "Only existing filters can be used as parent filters."
            : validationResult === "INVALID_METADATA"
                ? "Some of the parents provided cannot be used because the 'metadata' for parent filter are missing."
                : "Some of the parents provided cannot be used because the 'over' parameter is invalid for the target filter.";
        throw invalidArgumentsProvided(ctx, cmd, message);
    }
    yield put(tabsActions.setAttributeFilterParents({
        filterLocalId,
        parentFilters,
    }));
    const changedFilter = yield select(selectFilterContextAttributeFilterItemByLocalId(filterLocalId));
    invariant(changedFilter, "Inconsistent state in attributeFilterSetParentCommandHandler");
    yield dispatchDashboardEvent(attributeFilterParentChanged(ctx, changedFilter, cmd.correlationId));
    yield call(dispatchFilterContextChanged, ctx, cmd);
}
//# sourceMappingURL=setAttributeFilterParentHandler.js.map