// (C) 2020-2025 GoodData Corporation
import { compact, isEqual, uniqWith } from "lodash-es";
import { isCrossFiltering, isDrillToLegacyDashboard, } from "@gooddata/sdk-model";
import { isDrillIntersectionAttributeItem } from "@gooddata/sdk-ui";
import { getDrillOriginLocalIdentifier, isDrillConfigured, isDrillDownIntersectionIgnoredAttributesForHierarchy, } from "../../../_staging/drills/drillingUtils.js";
import { isDrillToUrl } from "../types.js";
/**
 * @internal
 */
export function getDrillDownTitle(drillDefinition, drillEvent, drillDownIntersectionIgnoredAttributes, drillTargetDisplayForm) {
    const localIdentifier = getDrillOriginLocalIdentifier(drillDefinition);
    if (drillDownIntersectionIgnoredAttributes) {
        const drillDownIntersectionIgnoredAttributesForCurrentHierarchy = drillDownIntersectionIgnoredAttributes?.find((ignored) => {
            return isDrillDownIntersectionIgnoredAttributesForHierarchy(ignored, drillDefinition.hierarchyRef);
        });
        const ignoredAttributesLocalIdentifiers = drillDownIntersectionIgnoredAttributesForCurrentHierarchy?.ignoredAttributes ?? [];
        if (ignoredAttributesLocalIdentifiers.includes(localIdentifier)) {
            return drillDefinition.title ?? drillTargetDisplayForm?.title ?? "";
        }
    }
    return getDrillOriginAttributeElementTitle(localIdentifier, drillEvent);
}
/**
 * @internal
 */
export function getDrillOriginAttributeElementTitle(drillOriginLocalId, drillEvent) {
    return (drillEvent.drillContext.intersection || [])
        .map((intersectionElement) => intersectionElement.header)
        .filter(isDrillIntersectionAttributeItem)
        .filter((intersectionAttributeItem) => intersectionAttributeItem.attributeHeader.localIdentifier === drillOriginLocalId)
        .map((intersectionAttributeItem) => intersectionAttributeItem.attributeHeaderItem.name)[0];
}
/**
 * Get total number of IDrillToUrl
 * @internal
 */
export function getTotalDrillToUrlCount(drillDefinition) {
    return drillDefinition.filter(isDrillToUrl).length;
}
/**
 * Implicit drill (currently IDrillDownDefinition and implicit IDrillToAttributeUrl) has lower priority,
 * so needs to be removed when other drill config exists for the same attribute
 *
 * @internal
 */
export function filterDrillFromAttributeByPriority(drillDefinitions, configuredDrills = []) {
    // need create deep equal unique drills array because we can get same two drills that are configured and implicit e.g. IDrillToAttributeUrl
    // and is not able to configure more drills on one origin
    // configured once has higher priority
    const uniqueDrillDefinitions = uniqWith(drillDefinitions, isEqual);
    const drillOriginsWithoutImplicitDrills = compact(uniqueDrillDefinitions.map((drill) => {
        if (!isDrillToLegacyDashboard(drill) && isDrillConfigured(drill, configuredDrills)) {
            return getDrillOriginLocalIdentifier(drill);
        }
        return undefined;
    }));
    return uniqueDrillDefinitions.filter((drill) => {
        if (isDrillToLegacyDashboard(drill) || isCrossFiltering(drill)) {
            return true;
        }
        if (!isDrillConfigured(drill, configuredDrills)) {
            return !drillOriginsWithoutImplicitDrills.includes(getDrillOriginLocalIdentifier(drill));
        }
        return true;
    });
}
//# sourceMappingURL=drillDownUtils.js.map