// (C) 2020-2026 GoodData Corporation
import { isArbitraryAttributeFilter, isLocalIdRef, isMatchAttributeFilter, isRankingFilter, } from "@gooddata/sdk-model";
import { sourceInsightFilterObjRef as getSourceInsightFilterObjRef } from "../../../../../../_staging/drills/drillingUtils.js";
import { canTransferAttributeFilterByDisplayForm, getDateDatasetTitle, getDisabledOptionProps, getDisabledOptionPropsForTransferResult, getDisplayFormTitle, getMeasureTitleFromSourceInsightMeasures, hasMatchingTargetDashboardDateFilter, sourceFilterOptionId, } from "../drillFiltersConfigUtils.js";
import { messages } from "../messages.js";
export function mapSourceInsightFilterToOption({ sourceInsightFilter, allCatalogDisplayFormsMap, allCatalogDateDatasets, sourceInsightMeasures, allCatalogMeasures, targetDashboardFilters, targetDashboardAttributeFilters, targetDashboardTextAttributeFilters, targetDashboardAttributeFilterConfigs, isDrillDown, isDrillToDashboard, intl, }) {
    const sourceFilterObjRef = getSourceInsightFilterObjRef(sourceInsightFilter);
    if (!sourceFilterObjRef) {
        return undefined;
    }
    const disabled = getDisabledOptionProps(isDrillDown, intl.formatMessage(messages.drillDownInsightFilterTooltip), true);
    const rankingFilterDisabled = isDrillDown
        ? disabled
        : getDisabledOptionProps(true, isDrillToDashboard
            ? intl.formatMessage(messages.drillToDashboardRankingFilterTooltip)
            : intl.formatMessage(messages.rankingFilterTooltip), false);
    if (sourceFilterObjRef.type === "attributeFilter") {
        const displayFormRef = sourceFilterObjRef.label;
        if (isLocalIdRef(displayFormRef)) {
            return undefined;
        }
        const isSourceTextFilter = isArbitraryAttributeFilter(sourceInsightFilter) || isMatchAttributeFilter(sourceInsightFilter);
        const transferResult = canTransferAttributeFilterByDisplayForm(displayFormRef, isSourceTextFilter, targetDashboardAttributeFilters, targetDashboardAttributeFilterConfigs, targetDashboardTextAttributeFilters);
        return {
            id: sourceFilterOptionId(sourceFilterObjRef),
            title: getDisplayFormTitle({
                displayFormRef,
                allCatalogDisplayFormsMap,
            }),
            ...disabled,
            ...getDisabledOptionPropsForTransferResult(isDrillToDashboard, transferResult, intl),
            sourceInsightFilterObjRef: sourceFilterObjRef,
        };
    }
    if (sourceFilterObjRef.type === "dateFilter") {
        const dateDatasetRef = sourceFilterObjRef.dataSet;
        if (isLocalIdRef(dateDatasetRef)) {
            return undefined;
        }
        const hasMatchingTargetDateFilter = hasMatchingTargetDashboardDateFilter(dateDatasetRef, targetDashboardFilters);
        return {
            id: sourceFilterOptionId(sourceFilterObjRef),
            title: getDateDatasetTitle({
                datasetRef: dateDatasetRef,
                allCatalogDateDatasets,
            }),
            ...disabled,
            ...getDisabledOptionProps(isDrillToDashboard && !hasMatchingTargetDateFilter, intl.formatMessage(messages.drillToDashboardDashboardFilterTooltip), false),
            sourceInsightFilterObjRef: sourceFilterObjRef,
        };
    }
    if (sourceFilterObjRef.type === "measureValueFilter") {
        const measureRef = sourceFilterObjRef.measure;
        const measureTitleFromInsight = getMeasureTitleFromSourceInsightMeasures(sourceInsightMeasures, measureRef, allCatalogMeasures, intl);
        return {
            id: sourceFilterOptionId(sourceFilterObjRef),
            title: measureTitleFromInsight ?? "",
            ...(isDrillToDashboard
                ? getDisabledOptionProps(true, intl.formatMessage(messages.drillToDashboardMetricFilterTooltip), false)
                : disabled),
            sourceInsightFilterObjRef: sourceFilterObjRef,
        };
    }
    if (sourceFilterObjRef.type === "rankingFilter" && isRankingFilter(sourceInsightFilter)) {
        const measureRef = sourceFilterObjRef.measure;
        const measureTitleFromInsight = getMeasureTitleFromSourceInsightMeasures(sourceInsightMeasures, measureRef, allCatalogMeasures, intl);
        const rankingPreviewMessage = sourceInsightFilter.rankingFilter.operator === "TOP"
            ? messages.rankingFilterPreviewTopWithoutAttributePlain
            : messages.rankingFilterPreviewBottomWithoutAttributePlain;
        const title = intl.formatMessage(rankingPreviewMessage, {
            value: sourceInsightFilter.rankingFilter.value,
            measure: measureTitleFromInsight ?? "",
        });
        return {
            id: sourceFilterOptionId(sourceFilterObjRef),
            title,
            ...rankingFilterDisabled,
            sourceInsightFilterObjRef: sourceFilterObjRef,
        };
    }
    return undefined;
}
//# sourceMappingURL=mapSourceInsightFilterToOption.js.map