// (C) 2020-2026 GoodData Corporation
import { isAllTimeDashboardDateFilter, isDashboardArbitraryAttributeFilter, isDashboardAttributeFilterItem, isDashboardDateFilter, isDashboardMatchAttributeFilter, isDashboardMeasureValueFilter, newAbsoluteDateFilter, newAllTimeFilter, newArbitraryAttributeFilter, newMatchAttributeFilter, newNegativeAttributeFilter, newPositiveAttributeFilter, newRelativeDateFilter, } from "@gooddata/sdk-model";
/**
 * Gets {@link IDashboardFilter} items for filters specified in given filterContext in relation to the given widget.
 *
 * @param filterContext - filter context to get filters for
 * @param widget - widget to use to get dateDataSet for date filters
 * @public
 */
export function filterContextToDashboardFiltersByWidget(filterContext, widget) {
    if (!filterContext) {
        return [];
    }
    return filterContextItemsToDashboardFiltersByWidget(filterContext.filters, widget);
}
/**
 * Gets {@link IDashboardFilter} items for filters specified in given filterContext in relation to the
 * given dateDataSet.
 *
 * @param filterContext - filter context to get filters for
 * @param dateDataSet - widget to use to get dateDataSet for date filters
 * @public
 */
export function filterContextToDashboardFiltersByDateDataSet(filterContext, dateDataSet) {
    if (!filterContext) {
        return [];
    }
    return filterContextItemsToDashboardFiltersByDateDataSet(filterContext.filters, dateDataSet);
}
/**
 * Converts {@link @gooddata/sdk-backend-spi#IDashboardAttributeFilter} to {@link @gooddata/sdk-model#IAttributeFilter} instance.
 *
 * @param filter - filter context attribute filter to convert
 * @deprecated Use {@link dashboardAttributeFilterItemToAttributeFilter} instead,
 * which supports all attribute filter types including text filters.
 * @public
 */
export function dashboardAttributeFilterToAttributeFilter(filter) {
    if (filter.attributeFilter.negativeSelection) {
        return newNegativeAttributeFilter(filter.attributeFilter.displayForm, filter.attributeFilter.attributeElements, filter.attributeFilter.localIdentifier);
    }
    return newPositiveAttributeFilter(filter.attributeFilter.displayForm, filter.attributeFilter.attributeElements, filter.attributeFilter.localIdentifier);
}
/**
 * Converts any {@link @gooddata/sdk-model#DashboardAttributeFilterItem} to {@link @gooddata/sdk-model#IAttributeFilter} instance.
 *
 * @remarks
 * Handles all attribute filter types: element-based (positive/negative selection),
 * arbitrary value filters, and match (text) filters.
 *
 * @param filter - dashboard attribute filter item to convert
 * @public
 */
export function dashboardAttributeFilterItemToAttributeFilter(filter) {
    if (isDashboardArbitraryAttributeFilter(filter)) {
        const { displayForm, values, negativeSelection, localIdentifier } = filter.arbitraryAttributeFilter;
        return newArbitraryAttributeFilter(displayForm, values, negativeSelection, localIdentifier);
    }
    if (isDashboardMatchAttributeFilter(filter)) {
        const { displayForm, operator, literal, caseSensitive, negativeSelection, localIdentifier } = filter.matchAttributeFilter;
        return newMatchAttributeFilter(displayForm, operator, literal, { caseSensitive, negativeSelection }, localIdentifier);
    }
    if (filter.attributeFilter.negativeSelection) {
        return newNegativeAttributeFilter(filter.attributeFilter.displayForm, filter.attributeFilter.attributeElements, filter.attributeFilter.localIdentifier);
    }
    return newPositiveAttributeFilter(filter.attributeFilter.displayForm, filter.attributeFilter.attributeElements, filter.attributeFilter.localIdentifier);
}
/**
 * Converts {@link @gooddata/sdk-backend-spi#IDashboardDateFilter} to {@link @gooddata/sdk-model#IDateFilter} instance.
 *
 * @param filter - filter context attribute filter to convert
 * @param widget - widget to use to get dateDataSet for date filters
 * @public
 */
export function dashboardDateFilterToDateFilterByWidget(filter, widget) {
    if (isAllTimeDashboardDateFilter(filter)) {
        const dataSet = widget
            ? filter.dateFilter.dataSet || widget.dateDataSet
            : filter.dateFilter.dataSet;
        return newAllTimeFilter(dataSet, filter.dateFilter.localIdentifier, filter.dateFilter.emptyValueHandling);
    }
    if (filter.dateFilter.type === "relative") {
        return newRelativeDateFilter(widget ? filter.dateFilter.dataSet || widget.dateDataSet : filter.dateFilter.dataSet, filter.dateFilter.granularity, numberOrStringToNumber(filter.dateFilter.from), numberOrStringToNumber(filter.dateFilter.to), filter.dateFilter.localIdentifier, filter.dateFilter.boundedFilter, filter.dateFilter.emptyValueHandling);
    }
    else {
        return newAbsoluteDateFilter(widget ? filter.dateFilter.dataSet || widget.dateDataSet : filter.dateFilter.dataSet, filter.dateFilter.from.toString(), filter.dateFilter.to.toString(), filter.dateFilter.localIdentifier, filter.dateFilter.emptyValueHandling);
    }
}
/**
 * Converts {@link @gooddata/sdk-backend-spi#IDashboardDateFilter} to {@link @gooddata/sdk-model#IDateFilter} instance.
 *
 * @param filter - filter context attribute filter to convert
 * @param dateDataSet - date data set to define {@link @gooddata/sdk-model#IDateFilter}
 * @public
 */
export function dashboardDateFilterToDateFilterByDateDataSet(filter, dateDataSet) {
    if (isAllTimeDashboardDateFilter(filter)) {
        return newAllTimeFilter(dateDataSet, filter.dateFilter.localIdentifier, filter.dateFilter.emptyValueHandling);
    }
    if (filter.dateFilter.type === "relative") {
        return newRelativeDateFilter(dateDataSet, filter.dateFilter.granularity, numberOrStringToNumber(filter.dateFilter.from), numberOrStringToNumber(filter.dateFilter.to), filter.dateFilter.localIdentifier, filter.dateFilter.boundedFilter, filter.dateFilter.emptyValueHandling);
    }
    else {
        return newAbsoluteDateFilter(dateDataSet, filter.dateFilter.from.toString(), filter.dateFilter.to.toString(), filter.dateFilter.localIdentifier, filter.dateFilter.emptyValueHandling);
    }
}
/**
 * Converts {@link @gooddata/sdk-model#IDashboardMeasureValueFilter} to {@link @gooddata/sdk-model#IMeasureValueFilter} instance.
 *
 * @remarks
 * Dashboard measure value filters always reference a catalog metric via `ObjRef`, which is a valid
 * `ObjRefInScope`. No dimensionality is set on the resulting execution filter — the backend derives
 * granularity from the widget automatically. Conditions are passed through unchanged.
 *
 * @param filter - dashboard measure value filter to convert
 * @public
 */
export function dashboardMeasureValueFilterToMeasureValueFilter(filter) {
    const { measure, localIdentifier, conditions } = filter.dashboardMeasureValueFilter;
    return {
        measureValueFilter: {
            measure,
            localIdentifier,
            ...(conditions && conditions.length > 0 ? { conditions } : {}),
        },
    };
}
/**
 * Gets {@link IDashboardFilter} items for filters specified as {@link @gooddata/sdk-backend-spi#FilterContextItem} instances.
 *
 * @param filterContextItems - filter context items to get filters for
 * @param widget - widget to use to get dateDataSet for date filters
 * @public
 */
export function filterContextItemsToDashboardFiltersByWidget(filterContextItems, widget) {
    return filterContextItems
        .filter((filter) => isDashboardAttributeFilterItem(filter) ||
        isDashboardDateFilter(filter) ||
        isDashboardMeasureValueFilter(filter))
        .map((filter) => {
        if (isDashboardAttributeFilterItem(filter)) {
            return dashboardAttributeFilterItemToAttributeFilter(filter);
        }
        if (isDashboardMeasureValueFilter(filter)) {
            return dashboardMeasureValueFilterToMeasureValueFilter(filter);
        }
        return dashboardDateFilterToDateFilterByWidget(filter, widget);
    });
}
/**
 * Gets {@link IDashboardFilter} items for filters specified as {@link @gooddata/sdk-backend-spi#FilterContextItem} instances.
 *
 * @param filterContextItems - filter context items to get filters for
 * @param widget - widget to use to get dateDataSet for date filters
 * @public
 */
export function filterContextItemsToDashboardFiltersByRichTextWidget(filterContextItems, widget) {
    return filterContextItems
        .filter((filter) => isDashboardAttributeFilterItem(filter) ||
        isDashboardDateFilter(filter) ||
        isDashboardMeasureValueFilter(filter))
        .map((filter) => {
        if (isDashboardAttributeFilterItem(filter)) {
            return dashboardAttributeFilterItemToAttributeFilter(filter);
        }
        if (isDashboardMeasureValueFilter(filter)) {
            return dashboardMeasureValueFilterToMeasureValueFilter(filter);
        }
        return dashboardDateFilterToDateFilterByWidget(filter, widget);
    })
        .filter(Boolean);
}
/**
 * Gets {@link IDashboardFilter} items for filters specified as {@link @gooddata/sdk-backend-spi#FilterContextItem} instances.
 *
 * @param filterContextItems - filter context items to get filters for
 * @param dateDataSet - date data set to define {@link @gooddata/sdk-model#IDateFilter}
 * @public
 */
export function filterContextItemsToDashboardFiltersByDateDataSet(filterContextItems, dateDataSet) {
    return filterContextItems
        .filter((filter) => isDashboardAttributeFilterItem(filter) ||
        isDashboardDateFilter(filter) ||
        isDashboardMeasureValueFilter(filter))
        .map((filter) => {
        if (isDashboardAttributeFilterItem(filter)) {
            return dashboardAttributeFilterItemToAttributeFilter(filter);
        }
        if (isDashboardMeasureValueFilter(filter)) {
            return dashboardMeasureValueFilterToMeasureValueFilter(filter);
        }
        return dashboardDateFilterToDateFilterByDateDataSet(filter, dateDataSet);
    });
}
function numberOrStringToNumber(input) {
    return typeof input === "string" ? Number.parseInt(input) : input;
}
//# sourceMappingURL=filterConverters.js.map