// (C) 2022-2026 GoodData Corporation
import { compact } from "lodash-es";
import { call, select } from "redux-saga/effects";
import { serializeObjRef } from "@gooddata/sdk-model";
import { invalidQueryArguments } from "../events/general.js";
import { createCachedQueryService } from "../store/_infra/queryService.js";
import { selectAttributeFilterDisplayFormsMap } from "../store/tabs/filterContext/filterContextSelectors.js";
export const QueryConnectingAttributesService = createCachedQueryService("GDC.DASH/QUERY.CONNECTING.ATTRIBUTES", queryService, (query) => {
    const { payload: { refs }, } = query;
    const serializedRefs = refs.flatMap((refs) => refs).map(serializeObjRef);
    return serializedRefs.join("&");
});
async function loadConnectingAttributes(ctx, attributeRefs) {
    const { backend, workspace } = ctx;
    return await backend.workspace(workspace).attributes().getCommonAttributesBatch(attributeRefs);
}
async function loadConnectingAttributesMeta(ctx, connectingAttributeRefs) {
    const { backend, workspace } = ctx;
    const attributeMetaObjects = await Promise.all(connectingAttributeRefs.map((refsEntry) => Promise.all(refsEntry.map((ref) => backend.workspace(workspace).attributes().getAttribute(ref)))));
    return attributeMetaObjects.map((entry) => {
        return entry.map((attributeMeta) => {
            return {
                title: attributeMeta.title,
                ref: attributeMeta.ref,
            };
        });
    });
}
function* mapDisplayFormsToAttributes(refs) {
    const attributeDisplayFormsMap = yield select(selectAttributeFilterDisplayFormsMap);
    return refs.map((displayFormRefsEntry) => displayFormRefsEntry.map((displayFormRef) => attributeDisplayFormsMap.get(displayFormRef)?.attribute));
}
function* queryService(ctx, query) {
    const { payload: { refs }, correlationId, } = query;
    const attributeRefs = yield call(mapDisplayFormsToAttributes, refs);
    const nonEmptyAttributesRef = compact(attributeRefs.map((refsEntry) => compact(refsEntry)));
    if (refs.length !== nonEmptyAttributesRef.length) {
        throw invalidQueryArguments(ctx, `Cannot find attributes for given displayForms`, correlationId);
    }
    const connectingAttributesRefs = yield call(loadConnectingAttributes, ctx, nonEmptyAttributesRef);
    const connectingAttributes = yield call(loadConnectingAttributesMeta, ctx, connectingAttributesRefs);
    return connectingAttributes;
}
//# sourceMappingURL=queryConnectingAttributes.js.map