"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDownloadPivotOptions = exports.getDownloadPivotConfig = exports.getPivotConfig = void 0;
const savedCharts_1 = require("../types/savedCharts");
/**
 * Split all hidden field IDs from a table chart config into dimension and metric
 * buckets. The `dimensionFieldIds` set is derived from the metricQuery so we can
 * classify each hidden field without needing a full ItemsMap.
 *
 * Fields that appear in metricQuery.dimensions are treated as dimensions; all
 * others (metrics, table calculations, pivot dims, etc.) are treated as metrics
 * for the purpose of the hidden-field filter in convertSqlPivotedRowsToPivotData.
 */
function splitHiddenTableFieldsByKind(tableChartConfig, metricQuery) {
    const allHidden = (0, savedCharts_1.getHiddenTableFields)(tableChartConfig);
    const dimensionSet = new Set(metricQuery.dimensions);
    const hiddenDimensions = [];
    const hiddenMetrics = [];
    for (const fieldId of allHidden) {
        if (dimensionSet.has(fieldId)) {
            hiddenDimensions.push(fieldId);
        }
        else {
            hiddenMetrics.push(fieldId);
        }
    }
    return { hiddenDimensions, hiddenMetrics };
}
const getTablePivotConfig = (tableChartConfig, pivotConfig, tableConfig, metricQuery) => {
    if (!pivotConfig || pivotConfig.columns.length === 0) {
        return undefined;
    }
    if (metricQuery) {
        const { hiddenDimensions, hiddenMetrics } = splitHiddenTableFieldsByKind(tableChartConfig, metricQuery);
        return {
            pivotDimensions: pivotConfig.columns,
            metricsAsRows: tableChartConfig.config?.metricsAsRows ?? false,
            ...(hiddenMetrics.length > 0 && {
                hiddenMetricFieldIds: hiddenMetrics,
            }),
            ...(hiddenDimensions.length > 0 && {
                hiddenDimensionFieldIds: hiddenDimensions,
            }),
            columnOrder: tableConfig.columnOrder,
            rowTotals: tableChartConfig.config?.showRowCalculation ?? false,
            columnTotals: tableChartConfig.config?.showColumnCalculation ?? false,
        };
    }
    // Fallback: no metricQuery available — put all hidden fields in hiddenMetricFieldIds
    // for backward compatibility (previous behaviour).
    return {
        pivotDimensions: pivotConfig.columns,
        metricsAsRows: tableChartConfig.config?.metricsAsRows ?? false,
        hiddenMetricFieldIds: (0, savedCharts_1.getHiddenTableFields)(tableChartConfig),
        columnOrder: tableConfig.columnOrder,
        rowTotals: tableChartConfig.config?.showRowCalculation ?? false,
        columnTotals: tableChartConfig.config?.showColumnCalculation ?? false,
    };
};
const getCartesianPivotConfig = (pivotConfig) => {
    if (!pivotConfig || pivotConfig.columns.length === 0) {
        return undefined;
    }
    return {
        pivotDimensions: pivotConfig.columns,
        metricsAsRows: false,
    };
};
const getPivotConfig = (savedChart) => {
    switch (savedChart.chartConfig.type) {
        case savedCharts_1.ChartType.TABLE:
            return getTablePivotConfig(savedChart.chartConfig, savedChart.pivotConfig, savedChart.tableConfig, savedChart.metricQuery);
        case savedCharts_1.ChartType.CARTESIAN:
            return getCartesianPivotConfig(savedChart.pivotConfig);
        default:
            return undefined;
    }
};
exports.getPivotConfig = getPivotConfig;
const getDownloadPivotConfig = (savedChart, exportPivotedData = true) => {
    switch (savedChart.chartConfig.type) {
        case savedCharts_1.ChartType.TABLE:
            return (0, exports.getPivotConfig)(savedChart);
        case savedCharts_1.ChartType.CARTESIAN:
            return exportPivotedData ? (0, exports.getPivotConfig)(savedChart) : undefined;
        default:
            return undefined;
    }
};
exports.getDownloadPivotConfig = getDownloadPivotConfig;
const getDownloadPivotOptions = (savedChart, exportPivotedData = true) => ({
    pivotConfig: (0, exports.getDownloadPivotConfig)(savedChart, exportPivotedData),
    exportPivotedData: savedChart.chartConfig.type === savedCharts_1.ChartType.CARTESIAN
        ? exportPivotedData
        : true,
});
exports.getDownloadPivotOptions = getDownloadPivotOptions;
//# sourceMappingURL=pivotConfig.js.map