// (C) 2023-2026 GoodData Corporation
import { areObjRefsEqual, isAttributeDescriptor, } from "@gooddata/sdk-model";
import { isDrillIntersectionAttributeItem, } from "@gooddata/sdk-ui";
import { generateFilterLocalIdentifier } from "../../../store/_infra/generators.js";
/**
 *  For correct drill intersection that should be converted into AttributeFilters must be drill intersection:
 *  1. AttributeItem
 *  2. Not a date attribute
 */
function filterIntersection(intersection, dateDataSetsAttributesRefs) {
    const attributeItem = isDrillIntersectionAttributeItem(intersection) ? intersection : undefined;
    const ref = attributeItem?.attributeHeader?.formOf?.ref;
    return ref ? !dateDataSetsAttributesRefs.some((ddsRef) => areObjRefsEqual(ddsRef, ref)) : false;
}
export function convertIntersectionToAttributeFilters(intersection, dateDataSetsAttributesRefs, enableAliasTitles = false, filtersCount = 0) {
    return intersection
        .map((i) => i.header)
        .filter((i) => filterIntersection(i, dateDataSetsAttributesRefs))
        .filter(isDrillIntersectionAttributeItem)
        .reduce((result, h) => {
        const ref = h.attributeHeader.ref;
        const elementValue = h.attributeHeaderItem.uri;
        const titleObj = enableAliasTitles ? { title: h.attributeHeader.formOf?.name } : {};
        result.push({
            attributeFilter: {
                attributeFilter: {
                    attributeElements: { uris: [elementValue] },
                    displayForm: ref,
                    negativeSelection: false,
                    localIdentifier: generateFilterLocalIdentifier(ref, filtersCount + result.length),
                    ...titleObj,
                },
            },
            primaryLabel: h.attributeHeader.primaryLabel,
        });
        return result;
    }, []);
}
/**
 * @internal
 */
export function removeIgnoredValuesFromDrillIntersection(intersection, drillIntersectionIgnoredAttributes) {
    return intersection.filter((i) => {
        if (isAttributeDescriptor(i.header) || isDrillIntersectionAttributeItem(i.header)) {
            return !drillIntersectionIgnoredAttributes?.includes(i.header.attributeHeader.localIdentifier);
        }
        return true;
    });
}
//# sourceMappingURL=intersectionUtils.js.map