// (C) 2021-2026 GoodData Corporation
import { isEmpty } from "lodash-es";
import { call } from "redux-saga/effects";
import { invariant } from "ts-invariant";
import { dashboardAttributeFilterItemDisplayForm, isDashboardAttributeFilterItem, objRefToString, } from "@gooddata/sdk-model";
import { resolveDisplayFormMetadata } from "./displayFormResolver.js";
/**
 * This generator function resolves display form metadata objects for all attribute filters in the provided `filters`
 * parameter. The resolver will first check in-memory `displayForms` mapping; if some used display forms are not
 * found, it will consult the backend.
 *
 * @param ctx - dashboard context where the resolution is being done
 * @param filters - list of dashboard filters; resolver will extract just the attribute filters from here
 * @param displayForms - in-memory mapping of known display forms to use during the initial lookup; if not specified, the
 *  code will obtain all catalog display forms; note: this parameter is really only useful during the dashboard initialization
 *  where the catalog is not yet set. once the dashboard is initialized, the parameter is not needed
 */
export function* resolveFilterDisplayForms(ctx, filters, displayAsLabels, displayForms) {
    const displayFormRefs = filters
        .filter(isDashboardAttributeFilterItem)
        .map((filter) => dashboardAttributeFilterItemDisplayForm(filter));
    const resolvedDisplayForms = yield call(resolveDisplayFormMetadata, ctx, [...displayFormRefs, ...displayAsLabels], displayForms);
    // TODO: this is too strict; instead of exploding, the resolver should communicate that some filters are invalid so
    //  that the upstream code can remove the filter from the filter context
    invariant(isEmpty(resolvedDisplayForms.missing), `Unable to resolve display forms used in filter context filters: ${resolvedDisplayForms.missing
        .map((m) => objRefToString(m))
        .join(", ")}.`);
    return Array.from(resolvedDisplayForms.resolved.values());
}
//# sourceMappingURL=filterResolver.js.map