// (C) 2025-2026 GoodData Corporation
import { isFilter, isFilterContextItem, isWidget, mergeFilters, } from "@gooddata/sdk-model";
import { filterContextItemsToDashboardFiltersByWidget } from "../../../converters/filterConverters.js";
export function prepareCsvRawExecutionDefinition(executionDefinition, csvRawRequest, insight, widget, commonDateFilterId) {
    const filters = csvRawRequest?.content?.filters;
    if (!executionDefinition || !filters?.length || !insight) {
        return executionDefinition;
    }
    const executionFilters = exportRequestFiltersToExecutionFilters(filters, widget);
    if (!executionFilters.length) {
        return executionDefinition;
    }
    const baseFilters = insight.insight?.filters ?? [];
    const mergedFilters = mergeFilters(baseFilters, executionFilters, commonDateFilterId);
    return {
        ...executionDefinition,
        filters: mergedFilters,
    };
}
function exportRequestFiltersToExecutionFilters(filters, widget) {
    const executionFilters = filters.filter(isFilter);
    const filterContextItems = filters.filter(isFilterContextItem);
    if (!filterContextItems.length) {
        return executionFilters;
    }
    const filterableWidget = isWidget(widget) ? widget : undefined;
    const dashboardFilters = filterContextItemsToDashboardFiltersByWidget(filterContextItems, filterableWidget);
    return [...executionFilters, ...dashboardFilters];
}
//# sourceMappingURL=csvRawExecutionDefinition.js.map