// (C) 2020-2026 GoodData Corporation
import { dashboardAttributeFilterItemDisplayForm, dashboardAttributeFilterItemLocalIdentifier, dashboardAttributeFilterItemTitle, dashboardFilterObjRef, dashboardMeasureValueFilterLocalIdentifier, isDashboardAttributeFilter, isDashboardAttributeFilterItem, isDashboardDateFilter, isDashboardMeasureValueFilter, } from "@gooddata/sdk-model";
import { canTransferDashboardAttributeFilter, canTransferTextAttributeFilter, getCatalogMeasureTitle, getDashboardDateFilterCustomTitle, getDateDatasetTitle, getDisabledOptionProps, getDisabledOptionPropsForTransferResult, getDisplayFormTitle, hasCompatibleTargetInsightMeasureValueFilter, hasMatchingTargetDashboardDateFilter, hasMatchingTargetDashboardMeasureValueFilter, } from "../drillFiltersConfigUtils.js";
import { messages } from "../messages.js";
export function mapDashboardFilterToOption({ dashboardFilter, allCatalogDisplayFormsMap, allCatalogMeasures, allCatalogDateDatasets, dateFilterConfigOverride, allDateFilterConfigsOverrides, sourceDashboardAttributeFilterConfigs, targetDashboardFilters, targetDashboardAttributeFilters, targetDashboardTextAttributeFilters, targetDashboardMeasureValueFilters, targetInsightCompatibleMeasureValueFilters, targetDashboardAttributeFilterConfigs, isDrillDown, isDrillToInsight, isDrillToDashboard, intl, }) {
    const disabled = getDisabledOptionProps(isDrillDown, intl.formatMessage(messages.drillDownDashboardFilterTooltip), true);
    if (isDashboardAttributeFilterItem(dashboardFilter)) {
        const displayFormRef = dashboardAttributeFilterItemDisplayForm(dashboardFilter);
        const localIdentifier = dashboardAttributeFilterItemLocalIdentifier(dashboardFilter);
        const customTitle = dashboardAttributeFilterItemTitle(dashboardFilter);
        if (!localIdentifier) {
            return undefined;
        }
        // In case of drill to dashboard, we need to check if the attribute filter is available
        // on the target dashboard. Otherwise disable the option.
        // This considers both list and text filters on the target, and selection type config.
        const transferResult = isDashboardAttributeFilter(dashboardFilter)
            ? canTransferDashboardAttributeFilter(dashboardFilter, sourceDashboardAttributeFilterConfigs, targetDashboardAttributeFilters, targetDashboardAttributeFilterConfigs, targetDashboardTextAttributeFilters, allCatalogDisplayFormsMap)
            : canTransferTextAttributeFilter(dashboardFilter, targetDashboardAttributeFilters, targetDashboardAttributeFilterConfigs, targetDashboardTextAttributeFilters);
        return {
            id: localIdentifier,
            title: customTitle ??
                getDisplayFormTitle({
                    displayFormRef,
                    allCatalogDisplayFormsMap,
                }),
            ...disabled,
            ...getDisabledOptionPropsForTransferResult(isDrillToDashboard, transferResult, intl),
        };
    }
    if (isDashboardDateFilter(dashboardFilter)) {
        const localIdentifier = dashboardFilter.dateFilter.localIdentifier;
        if (!localIdentifier) {
            return undefined;
        }
        const datasetRef = dashboardFilter.dateFilter.dataSet;
        // common date filter
        if (!datasetRef) {
            return {
                id: localIdentifier,
                title: dateFilterConfigOverride?.filterName ??
                    intl.formatMessage(messages.commonDateFilterTitle),
                ...disabled,
            };
        }
        // date filter with dimension
        const customTitle = getDashboardDateFilterCustomTitle({
            datasetRef,
            allDateFilterConfigsOverrides,
        });
        const hasMatchingTargetDateFilter = hasMatchingTargetDashboardDateFilter(datasetRef, targetDashboardFilters);
        return {
            id: localIdentifier,
            title: customTitle ??
                getDateDatasetTitle({
                    datasetRef,
                    allCatalogDateDatasets,
                }),
            ...disabled,
            ...getDisabledOptionProps(isDrillToDashboard && !hasMatchingTargetDateFilter, intl.formatMessage(messages.drillToDashboardDashboardFilterTooltip), false),
        };
    }
    if (isDashboardMeasureValueFilter(dashboardFilter)) {
        const measure = dashboardFilterObjRef(dashboardFilter);
        const localIdentifier = dashboardMeasureValueFilterLocalIdentifier(dashboardFilter);
        const title = dashboardFilter.dashboardMeasureValueFilter.title;
        const hasMatchingTargetMeasureValueFilter = hasMatchingTargetDashboardMeasureValueFilter(measure, targetDashboardMeasureValueFilters);
        const compatible = hasCompatibleTargetInsightMeasureValueFilter(measure, targetInsightCompatibleMeasureValueFilters);
        return {
            id: localIdentifier,
            title: title ?? getCatalogMeasureTitle(measure, allCatalogMeasures),
            metricFilterMeasureRef: measure,
            ...disabled,
            ...getDisabledOptionProps(isDrillToDashboard && !hasMatchingTargetMeasureValueFilter, intl.formatMessage(messages.drillToDashboardDashboardFilterTooltip), false),
            ...getDisabledOptionProps(isDrillToInsight && !compatible, intl.formatMessage(messages.drillToInsightMeasureValueFilterTooltip), false),
        };
    }
    return undefined;
}
