"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const field_1 = require("../types/field");
const savedCharts_1 = require("../types/savedCharts");
const types_1 = require("../visualizations/types");
// Jest provides describe/it/expect globals
const derivePivotConfigFromChart_1 = require("./derivePivotConfigFromChart");
const derivePivotConfigFromChart_mock_1 = require("./derivePivotConfigFromChart.mock");
describe('derivePivotConfigurationFromChart', () => {
    it('derives pivot configuration for Cartesian charts with pivot config', () => {
        const savedChart = {
            chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
            pivotConfig: {
                columns: ['orders_status'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result).toEqual({
            indexColumn: [
                {
                    reference: 'payments_payment_method',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ],
            valuesColumns: [
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ],
            groupByColumns: [{ reference: 'orders_status' }],
            sortBy: [
                {
                    reference: 'payments_payment_method',
                    direction: types_1.SortByDirection.ASC,
                },
            ],
        });
    });
    it('derives pivot configuration for Cartesian charts with pivot config and multiple index columns', () => {
        const savedChart = {
            chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
            pivotConfig: {
                columns: ['orders_status'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQueryWithMultipleIndexColumns, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result).toEqual({
            indexColumn: [
                {
                    reference: 'payments_payment_method',
                    type: types_1.VizIndexType.CATEGORY,
                },
                // added by derivePivotConfigurationFromChart since this is not being pivoted on
                {
                    reference: 'customers_customer_id',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ],
            valuesColumns: [
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ],
            groupByColumns: [{ reference: 'orders_status' }],
            sortBy: [
                {
                    reference: 'payments_payment_method',
                    direction: types_1.SortByDirection.ASC,
                },
            ],
        });
    });
    it('returns undefined for Cartesian charts without pivot config', () => {
        const savedChart = {
            chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
            pivotConfig: undefined,
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result).toBeUndefined();
    });
    it('derives pivot configuration for Table charts with pivot config', () => {
        const tableChartConfig = {
            type: savedCharts_1.ChartType.TABLE,
            config: {},
        };
        // Pivot on payments_payment_method so orders_status becomes index column
        const savedChart = {
            chartConfig: tableChartConfig,
            pivotConfig: {
                columns: ['payments_payment_method'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result).toEqual({
            indexColumn: [
                {
                    reference: 'orders_status',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ],
            valuesColumns: [
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ],
            groupByColumns: [{ reference: 'payments_payment_method' }],
            sortBy: [
                {
                    reference: 'payments_payment_method',
                    direction: types_1.SortByDirection.ASC,
                },
            ],
            metricsAsRows: undefined,
        });
    });
    it('passes metricsAsRows from Table chart config to pivot configuration', () => {
        const tableChartConfig = {
            type: savedCharts_1.ChartType.TABLE,
            config: {
                metricsAsRows: true,
            },
        };
        const savedChart = {
            chartConfig: tableChartConfig,
            pivotConfig: {
                columns: ['payments_payment_method'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result?.metricsAsRows).toBe(true);
    });
    it('keeps a pivot row metric as a query-time value column', () => {
        const savedChart = {
            chartConfig: {
                type: savedCharts_1.ChartType.TABLE,
                config: {},
            },
            pivotConfig: {
                columns: ['payments_payment_method'],
                rows: ['orders_status', 'payments_total_revenue'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result?.valuesColumns).toContainEqual({
            reference: 'payments_total_revenue',
            aggregation: types_1.VizAggregationOptions.ANY,
        });
        expect(result?.indexColumn).toEqual([
            {
                reference: 'orders_status',
                type: types_1.VizIndexType.CATEGORY,
            },
        ]);
    });
    it('does not include metricsAsRows for Cartesian charts', () => {
        const savedChart = {
            chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
            pivotConfig: {
                columns: ['orders_status'],
            },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        // Cartesian charts don't have metricsAsRows, so it should be undefined
        expect(result?.metricsAsRows).toBeUndefined();
    });
    it('returns undefined for unsupported chart types (PIE)', () => {
        const pieChartConfig = {
            type: savedCharts_1.ChartType.PIE,
            config: {},
        };
        const savedChart = {
            chartConfig: pieChartConfig,
            pivotConfig: { columns: ['payments_payment_method'] },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, derivePivotConfigFromChart_mock_1.mockMetricQuery, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result).toBeUndefined();
    });
    it('filters out sorts that are not present in pivot configuration', () => {
        const savedChart = {
            chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
            pivotConfig: {
                columns: ['orders_status'],
            },
        };
        const mq = {
            ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
            sorts: [
                { fieldId: 'payments_payment_method', descending: false }, // present as index
                { fieldId: 'payments_total_revenue', descending: true }, // present as value column
                { fieldId: 'non_existing_field', descending: false }, // should be filtered out
            ],
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
        expect(result?.sortBy).toEqual([
            {
                reference: 'payments_payment_method',
                direction: types_1.SortByDirection.ASC,
            },
            {
                reference: 'payments_total_revenue',
                direction: types_1.SortByDirection.DESC,
            },
        ]);
    });
    it('supports custom dimensions (bin) as valid xField and indexColumn in cartesian charts', () => {
        const items = {
            ...derivePivotConfigFromChart_mock_1.mockItems,
            amount_range: {
                id: 'amount_range',
                name: 'amount range',
                table: 'payments',
                type: field_1.CustomDimensionType.BIN,
                dimensionId: 'payments_amount',
                binType: field_1.BinType.FIXED_NUMBER,
                binNumber: 5,
            },
        };
        const mq = {
            ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
            dimensions: ['amount_range', 'payments_payment_method'],
            sorts: [
                {
                    fieldId: 'payments_total_revenue',
                    descending: true,
                },
            ],
        };
        const cartesianWithCustomX = {
            type: savedCharts_1.ChartType.CARTESIAN,
            config: {
                layout: {
                    xField: 'amount_range',
                    yField: ['payments_total_revenue'],
                },
                eChartsConfig: { series: [] },
            },
        };
        const savedChart = {
            chartConfig: cartesianWithCustomX,
            pivotConfig: { columns: ['payments_payment_method'] },
        };
        const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, items);
        expect(result).toEqual({
            indexColumn: [
                {
                    reference: 'amount_range',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ],
            valuesColumns: [
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ],
            groupByColumns: [{ reference: 'payments_payment_method' }],
            sortBy: [
                {
                    reference: 'payments_total_revenue',
                    direction: types_1.SortByDirection.DESC,
                },
            ],
        });
    });
    describe('Table Calculations Support', () => {
        const mockTableCalculation = {
            name: 'revenue_per_order',
            displayName: 'Revenue per Order',
            sql: '${payments_total_revenue} / ${orders_count}',
        };
        const mockTableCalculation2 = {
            name: 'revenue_growth',
            displayName: 'Revenue Growth',
            sql: '(${payments_total_revenue} - LAG(${payments_total_revenue})) / LAG(${payments_total_revenue})',
        };
        it('includes table calculations in valuesColumns for Table charts', () => {
            const tableChartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {},
            };
            const savedChart = {
                chartConfig: tableChartConfig,
                pivotConfig: {
                    columns: ['payments_payment_method'],
                },
            };
            const metricQueryWithTC = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                tableCalculations: [
                    mockTableCalculation,
                    mockTableCalculation2,
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryWithTC, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'revenue_per_order',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'revenue_growth',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
        it('includes table calculations in valuesColumns for Cartesian charts when in yField', () => {
            const cartesianChartWithTC = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        xField: 'payments_payment_method',
                        yField: ['payments_total_revenue', 'revenue_per_order'],
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const savedChart = {
                chartConfig: cartesianChartWithTC,
                pivotConfig: { columns: ['orders_status'] },
            };
            const metricQueryWithTC = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                tableCalculations: [mockTableCalculation],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryWithTC, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'revenue_per_order',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
        it('filters out table calculations not in yField for Cartesian charts', () => {
            const cartesianChartWithTC = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        xField: 'payments_payment_method',
                        yField: ['payments_total_revenue'], // Only metric, not the table calc
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const savedChart = {
                chartConfig: cartesianChartWithTC,
                pivotConfig: { columns: ['orders_status'] },
            };
            const metricQueryWithTC = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                tableCalculations: [mockTableCalculation], // TC exists but not in yField
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryWithTC, derivePivotConfigFromChart_mock_1.mockItems);
            // Should only include the metric that's actually in yField
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            expect(result?.indexColumn).toEqual([
                {
                    reference: 'payments_payment_method',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ]);
        });
        it('handles mixed metrics and table calculations in Table charts', () => {
            const tableChartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {},
            };
            const savedChart = {
                chartConfig: tableChartConfig,
                pivotConfig: {
                    columns: ['orders_status'],
                },
            };
            // Add another metric to mockItems
            const itemsWithMoreMetrics = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_count: {
                    sql: 'COUNT(${TABLE}.order_id)',
                    name: 'count',
                    type: field_1.MetricType.COUNT,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Order Count',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
            };
            const metricQueryWithMixed = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                metrics: ['payments_total_revenue', 'orders_count'],
                tableCalculations: [mockTableCalculation],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryWithMixed, itemsWithMoreMetrics);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'orders_count',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'revenue_per_order',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
        it('handles empty table calculations array', () => {
            const tableChartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {},
            };
            const savedChart = {
                chartConfig: tableChartConfig,
                pivotConfig: {
                    columns: ['payments_payment_method'],
                },
            };
            const metricQueryWithEmptyTC = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                tableCalculations: [],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryWithEmptyTC, derivePivotConfigFromChart_mock_1.mockItems);
            // Should only have the metric
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
        it('handles undefined table calculations', () => {
            const tableChartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {},
            };
            const savedChart = {
                chartConfig: tableChartConfig,
                pivotConfig: {
                    columns: ['payments_payment_method'],
                },
            };
            const metricQueryNoTC = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                // tableCalculations field is optional, so it can be undefined
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryNoTC, derivePivotConfigFromChart_mock_1.mockItems);
            // Should only have the metric
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
    });
    describe('Sort by metric not in viz (PROD-6906)', () => {
        it('puts sort-only metrics in sortOnlyColumns for Cartesian charts', () => {
            const itemsWithExtraMetric = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_count: {
                    sql: 'COUNT(${TABLE}.order_id)',
                    name: 'count',
                    type: field_1.MetricType.COUNT,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Order Count',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
            };
            const savedChart = {
                chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig, // yField: ['payments_total_revenue']
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                metrics: ['payments_total_revenue', 'orders_count'],
                sorts: [
                    { fieldId: 'orders_count', descending: true }, // sort by metric NOT in yField
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithExtraMetric);
            expect(result).toBeDefined();
            // orders_count should NOT be in valuesColumns
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // orders_count should be in sortOnlyColumns
            expect(result?.sortOnlyColumns).toEqual([
                {
                    reference: 'orders_count',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // The sort should be preserved
            expect(result?.sortBy).toEqual([
                {
                    reference: 'orders_count',
                    direction: types_1.SortByDirection.DESC,
                },
            ]);
        });
        it('puts sort-only table calculation in sortOnlyColumns', () => {
            const mockTableCalc = {
                name: 'revenue_per_order',
                displayName: 'Revenue per Order',
                sql: '${payments_total_revenue} / ${orders_count}',
            };
            const cartesianChartWithOneMetric = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        xField: 'payments_payment_method',
                        yField: ['payments_total_revenue'], // TC not in yField
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const savedChart = {
                chartConfig: cartesianChartWithOneMetric,
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                tableCalculations: [mockTableCalc],
                sorts: [{ fieldId: 'revenue_per_order', descending: true }],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result).toBeDefined();
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            expect(result?.sortOnlyColumns).toEqual([
                {
                    reference: 'revenue_per_order',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            expect(result?.sortBy).toEqual([
                {
                    reference: 'revenue_per_order',
                    direction: types_1.SortByDirection.DESC,
                },
            ]);
        });
        it('sort-only metric does not appear as index column', () => {
            const itemsWithExtraMetric = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_count: {
                    sql: 'COUNT(${TABLE}.order_id)',
                    name: 'count',
                    type: field_1.MetricType.COUNT,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Order Count',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
            };
            const savedChart = {
                chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                metrics: ['payments_total_revenue', 'orders_count'],
                sorts: [{ fieldId: 'orders_count', descending: true }],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithExtraMetric);
            expect(result).toBeDefined();
            // orders_count should be in valuesColumns, NOT indexColumn
            expect(result?.indexColumn).toEqual([
                {
                    reference: 'payments_payment_method',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ]);
        });
        it('sort by dimension does not add it to valuesColumns', () => {
            const savedChart = {
                chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig,
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                sorts: [
                    { fieldId: 'orders_status', descending: false }, // sort by dimension (groupBy)
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result).toBeDefined();
            // Dimension should NOT appear in valuesColumns
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // But the sort should still be preserved (it's a groupBy column)
            expect(result?.sortBy).toEqual([
                {
                    reference: 'orders_status',
                    direction: types_1.SortByDirection.ASC,
                },
            ]);
        });
        it('does not duplicate metrics already in yField when also in sorts', () => {
            const savedChart = {
                chartConfig: derivePivotConfigFromChart_mock_1.mockCartesianChartConfig, // yField: ['payments_total_revenue']
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                sorts: [
                    { fieldId: 'payments_total_revenue', descending: true },
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
    });
    describe('Stacked Bar Chart with Table Calculations', () => {
        it('does not include metrics as index columns when table calculation is on y-axis', () => {
            // This tests that metrics used by table calculations but not on the x-axis
            // are not incorrectly added as index columns, which would break stacking
            const itemsWithTableCalc = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_shipping_method: {
                    sql: '${TABLE}.shipping_method',
                    name: 'shipping_method',
                    type: field_1.DimensionType.STRING,
                    fieldType: field_1.FieldType.DIMENSION,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Shipping method',
                    hidden: false,
                    index: 0,
                    groups: [],
                },
                orders_order_source: {
                    sql: '${TABLE}.order_source',
                    name: 'order_source',
                    type: field_1.DimensionType.STRING,
                    fieldType: field_1.FieldType.DIMENSION,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Order source',
                    hidden: false,
                    index: 1,
                    groups: [],
                },
                orders_total_order_amount: {
                    sql: 'SUM(${TABLE}.amount)',
                    name: 'total_order_amount',
                    type: field_1.MetricType.SUM,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Total order amount',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
                _or_total: {
                    index: 0,
                    name: '_or_total',
                    displayName: '% or total',
                    sql: '${orders.total_order_amount} / sum(${orders.total_order_amount}) over(partition by ${orders.order_source})',
                },
            };
            const stackedBarChartConfig = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        xField: 'orders_shipping_method', // dimension on x-axis
                        yField: ['_or_total'], // table calculation on y-axis (uses the metric)
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const metricQueryForStackedBar = {
                exploreName: 'orders',
                dimensions: ['orders_shipping_method', 'orders_order_source'],
                metrics: ['orders_total_order_amount'], // metric used by table calc
                filters: {},
                sorts: [
                    { fieldId: 'orders_shipping_method', descending: false },
                ],
                limit: 500,
                tableCalculations: [
                    {
                        name: '_or_total',
                        displayName: '% or total',
                        sql: '${orders.total_order_amount} / sum(${orders.total_order_amount}) over(partition by ${orders.order_source})',
                    },
                ],
                additionalMetrics: [],
                metricOverrides: {},
            };
            const savedChart = {
                chartConfig: stackedBarChartConfig,
                pivotConfig: { columns: ['orders_order_source'] }, // pivot on order_source
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryForStackedBar, itemsWithTableCalc);
            expect(result).toBeDefined();
            // The metric (orders_total_order_amount) should NOT be in index columns
            // Only the x-axis dimension should be an index column
            expect(result?.indexColumn).toEqual([
                {
                    reference: 'orders_shipping_method',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ]);
            // The table calculation should be the value column
            expect(result?.valuesColumns).toEqual([
                {
                    reference: '_or_total',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // The pivot dimension should be the groupBy column
            expect(result?.groupByColumns).toEqual([
                { reference: 'orders_order_source' },
            ]);
        });
    });
    describe('Scatter Chart with Metrics on Both Axes', () => {
        it('includes x-axis metric as index column for grouped scatter charts', () => {
            // This tests the fix for GitHub issue #19911
            // Scatter charts can have metrics on both x and y axes
            const itemsWithTwoMetrics = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_count: {
                    sql: 'COUNT(${TABLE}.order_id)',
                    name: 'count',
                    type: field_1.MetricType.COUNT,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Order Count',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
            };
            const scatterChartConfig = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        xField: 'orders_count', // metric on x-axis
                        yField: ['payments_total_revenue'], // metric on y-axis
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const metricQueryForScatter = {
                exploreName: 'payments',
                dimensions: ['payments_payment_method'], // used as groupBy
                metrics: ['orders_count', 'payments_total_revenue'],
                filters: {},
                sorts: [],
                limit: 500,
                tableCalculations: [],
                additionalMetrics: [],
                metricOverrides: {},
            };
            const savedChart = {
                chartConfig: scatterChartConfig,
                pivotConfig: { columns: ['payments_payment_method'] }, // groupBy dimension
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQueryForScatter, itemsWithTwoMetrics);
            // The x-axis metric (orders_count) should be included as an index column
            expect(result).toBeDefined();
            expect(result?.indexColumn).toEqual([
                {
                    reference: 'orders_count',
                    type: types_1.VizIndexType.CATEGORY,
                },
            ]);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'payments_total_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            expect(result?.groupByColumns).toEqual([
                { reference: 'payments_payment_method' },
            ]);
        });
    });
    describe('Regressions for resolved pivot bugs', () => {
        it('preserves metricQuery.metrics order in valuesColumns for table charts with metricsAsRows + multiple dimensions (#19838)', () => {
            // https://github.com/lightdash/lightdash/issues/19838
            // Repro: select metrics in a specific order (Profit → Revenue → Sales),
            // pivot a table chart by a date dimension, enable "metrics as rows".
            // Bug: the metric rows came out in a different order from the user's
            // selection — there was no UI affordance to fix it.
            // Expectation: valuesColumns reflects metricQuery.metrics order
            // verbatim, not alphabetical or some other implicit ordering.
            const itemsWithThreeMetrics = {
                orders_order_date_day: {
                    sql: '${TABLE}.order_date_day',
                    name: 'order_date_day',
                    type: field_1.DimensionType.DATE,
                    index: 1,
                    label: 'Order date day',
                    table: 'orders',
                    groups: [],
                    hidden: false,
                    fieldType: field_1.FieldType.DIMENSION,
                    tableLabel: 'Orders',
                },
                orders_status: {
                    sql: '${TABLE}.status',
                    name: 'status',
                    type: field_1.DimensionType.STRING,
                    index: 2,
                    label: 'Status',
                    table: 'orders',
                    groups: [],
                    hidden: false,
                    fieldType: field_1.FieldType.DIMENSION,
                    tableLabel: 'Orders',
                },
                // Metric names are intentionally NOT in alphabetical order so a
                // regression that re-sorts them (alphabetical, by label, by SUM
                // before AVG, etc.) shows up clearly.
                orders_zzz_profit: {
                    sql: '${TABLE}.profit',
                    name: 'zzz_profit',
                    type: field_1.MetricType.SUM,
                    index: 1,
                    label: 'Profit',
                    table: 'orders',
                    groups: [],
                    hidden: false,
                    filters: [],
                    fieldType: field_1.FieldType.METRIC,
                    tableLabel: 'Orders',
                },
                orders_aaa_revenue: {
                    sql: '${TABLE}.revenue',
                    name: 'aaa_revenue',
                    type: field_1.MetricType.SUM,
                    index: 2,
                    label: 'Revenue',
                    table: 'orders',
                    groups: [],
                    hidden: false,
                    filters: [],
                    fieldType: field_1.FieldType.METRIC,
                    tableLabel: 'Orders',
                },
                orders_mmm_sales_count: {
                    sql: '${TABLE}.sales_count',
                    name: 'mmm_sales_count',
                    type: field_1.MetricType.COUNT,
                    index: 3,
                    label: 'Sales count',
                    table: 'orders',
                    groups: [],
                    hidden: false,
                    filters: [],
                    fieldType: field_1.FieldType.METRIC,
                    tableLabel: 'Orders',
                },
            };
            const metricQuery = {
                exploreName: 'orders',
                dimensions: ['orders_order_date_day', 'orders_status'],
                // Selection order: Profit, Revenue, Sales count.
                metrics: [
                    'orders_zzz_profit',
                    'orders_aaa_revenue',
                    'orders_mmm_sales_count',
                ],
                filters: {},
                sorts: [
                    { fieldId: 'orders_order_date_day', descending: false },
                ],
                limit: 500,
                tableCalculations: [],
                additionalMetrics: [],
                metricOverrides: {},
            };
            const tableChartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    metricsAsRows: true,
                },
            };
            const savedChart = {
                chartConfig: tableChartConfig,
                pivotConfig: { columns: ['orders_order_date_day'] },
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, metricQuery, itemsWithThreeMetrics);
            // metricsAsRows must round-trip onto the pivot configuration.
            expect(result?.metricsAsRows).toBe(true);
            // valuesColumns order is the contract: it must match the order in
            // metricQuery.metrics. If a future change re-sorts metrics by
            // label, by name, or by aggregation type, this test will fail.
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'orders_zzz_profit',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'orders_aaa_revenue',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
                {
                    reference: 'orders_mmm_sales_count',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
        });
        it('a sort-only table calculation that is not the xField stays out of indexColumn and valuesColumns', () => {
            // Locks the invariant that a TC referenced ONLY by a sort entry
            // (not on any chart axis) is routed exclusively to
            // sortOnlyColumns. The two pathways that could re-break this:
            //
            // 1. getIndexColumn re-introducing a `sortFieldIds.has(tc.name)`
            //    branch — that would make the TC enter indexColumn alongside
            //    its sortOnlyColumns entry, so the chart grows a phantom
            //    series for the sort field.
            // 2. The sortOnlyColumns filter dropping the indexColumn-aware
            //    exclusion — that would let an xField TC double up as both
            //    indexColumn and sortOnlyColumns when sorted by itself.
            //
            // The fixture: cartesian chart, x = a non-pivot dim, y = a
            // metric, plus a TC `y_day` used only for sort.
            const sortOnlyTc = {
                name: 'y_day',
                displayName: 'Day of year',
                type: field_1.TableCalculationType.NUMBER,
                sql: 'EXTRACT(DOY FROM ${orders.order_date_day})',
            };
            const itemsForRepro = {
                ...derivePivotConfigFromChart_mock_1.mockItems,
                orders_unique_order_count: {
                    sql: 'COUNT(DISTINCT ${TABLE}.order_id)',
                    name: 'unique_order_count',
                    type: field_1.MetricType.COUNT_DISTINCT,
                    fieldType: field_1.FieldType.METRIC,
                    table: 'orders',
                    tableLabel: 'Orders',
                    label: 'Unique order count',
                    hidden: false,
                    index: 0,
                    filters: [],
                    groups: [],
                },
            };
            const cartesianChartConfig = {
                type: savedCharts_1.ChartType.CARTESIAN,
                config: {
                    layout: {
                        // x-axis: a non-pivot dim. The y-axis carries ONLY the
                        // metric — the TC is not on the chart.
                        xField: 'payments_payment_method',
                        yField: ['orders_unique_order_count'],
                    },
                    eChartsConfig: { series: [] },
                },
            };
            const savedChart = {
                chartConfig: cartesianChartConfig,
                pivotConfig: { columns: ['orders_status'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                metrics: ['orders_unique_order_count'],
                tableCalculations: [sortOnlyTc],
                // The TC drives sort order but is not visualized.
                sorts: [{ fieldId: 'y_day', descending: false }],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsForRepro);
            expect(result).toBeDefined();
            // valuesColumns must contain exactly the y-axis metric. Anything
            // else (the TC creeping back in, an additional metric, etc.)
            // would make the rendered chart double its series count.
            expect(result?.valuesColumns).toHaveLength(1);
            expect(result?.valuesColumns).toEqual([
                {
                    reference: 'orders_unique_order_count',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // The TC is preserved as a sort-only column so PivotQueryBuilder
            // can still emit anchor CTEs that order rows by it.
            expect(result?.sortOnlyColumns).toEqual([
                {
                    reference: 'y_day',
                    aggregation: types_1.VizAggregationOptions.ANY,
                },
            ]);
            // sortBy still references the TC — the chart-render layer relies
            // on the value columns / sortOnlyColumns split, not on dropping
            // the sort entry.
            expect(result?.sortBy).toEqual([
                { reference: 'y_day', direction: types_1.SortByDirection.ASC },
            ]);
            // Hard guarantee: y_day must not appear as an index column
            // either. If it did, the chart would render it on the x-axis or
            // along a series dimension.
            const indexRefs = Array.isArray(result?.indexColumn)
                ? result.indexColumn.map((c) => c.reference)
                : [result?.indexColumn?.reference];
            expect(indexRefs).not.toContain('y_day');
        });
    });
    describe('sort-only pivot-column dimensions (hidden pivot dims) for Table charts', () => {
        // orders_status_priority is a helper dim that the user adds as a pivot column
        // purely to drive column sort order. It is hidden so users see only orders_status
        // as the pivot header, but the helper value still determines the column order.
        const itemsWithPriority = {
            ...derivePivotConfigFromChart_mock_1.mockItems,
            orders_status_priority: {
                sql: '${TABLE}.status_priority',
                name: 'status_priority',
                type: field_1.DimensionType.NUMBER,
                index: 5,
                label: 'Status priority',
                table: 'orders',
                groups: [],
                hidden: false, // not hidden in the explore — hidden in the chart config
                fieldType: field_1.FieldType.DIMENSION,
                tableLabel: 'Orders',
            },
        };
        it('routes a hidden pivot-column dim with a sort entry to sortOnlyDimensions (not groupByColumns)', () => {
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: { orders_status_priority: { visible: false } },
                    pivotDimensions: [
                        'orders_status',
                        'orders_status_priority',
                    ],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: {
                    columns: ['orders_status', 'orders_status_priority'],
                },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: [
                    'payments_payment_method',
                    'orders_status',
                    'orders_status_priority',
                ],
                metrics: ['payments_total_revenue'],
                sorts: [
                    { fieldId: 'orders_status_priority', descending: false },
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithPriority);
            expect(result).toBeDefined();
            // orders_status_priority should NOT be in groupByColumns
            expect(result?.groupByColumns?.some((c) => c.reference === 'orders_status_priority')).toBe(false);
            // orders_status SHOULD still be in groupByColumns
            expect(result?.groupByColumns?.some((c) => c.reference === 'orders_status')).toBe(true);
            // orders_status_priority SHOULD be in sortOnlyDimensions
            expect(result?.sortOnlyDimensions?.some((c) => c.reference === 'orders_status_priority')).toBe(true);
            // sortBy should still include orders_status_priority
            expect(result?.sortBy?.some((s) => s.reference === 'orders_status_priority')).toBe(true);
        });
        it('sets pivotColumnsOrder to the declared pivot-column order (visible + sort-only) so a hidden sort dim keeps its position (PROD-8159)', () => {
            // Declared order: payment_method (outer, visible), status_priority
            // (hidden sort helper), status (inner, visible). Hiding the helper
            // must not change column order, so pivotColumnsOrder preserves the
            // full declared sequence — the SQL builder then sorts the helper at
            // its declared (middle) position instead of hoisting it to the front.
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: { orders_status_priority: { visible: false } },
                    pivotDimensions: [
                        'payments_payment_method',
                        'orders_status_priority',
                        'orders_status',
                    ],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: {
                    columns: [
                        'payments_payment_method',
                        'orders_status_priority',
                        'orders_status',
                    ],
                },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: [
                    'payments_payment_method',
                    'orders_status',
                    'orders_status_priority',
                ],
                metrics: ['payments_total_revenue'],
                sorts: [
                    { fieldId: 'orders_status_priority', descending: false },
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithPriority);
            expect(result?.pivotColumnsOrder).toEqual([
                { reference: 'payments_payment_method' },
                { reference: 'orders_status_priority' },
                { reference: 'orders_status' },
            ]);
        });
        it('routes a hidden pivot-column dim with no sort entry to passthroughDimensions (PROD-7873)', () => {
            // Hidden + not sorted used to drop the dim from the query entirely,
            // which broke richText / image templates on visible fields that
            // reference the hidden field via `row.<table>.<field>.raw`.
            // Now the dim carries through via passthroughDimensions: it stays
            // in `group_by_query` SELECT/GROUP BY so its values reach the
            // result rows, but it does NOT appear as a pivot column header
            // and does NOT drive any ORDER BY.
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: { orders_status_priority: { visible: false } },
                    pivotDimensions: [
                        'orders_status',
                        'orders_status_priority',
                    ],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: {
                    columns: ['orders_status', 'orders_status_priority'],
                },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: [
                    'payments_payment_method',
                    'orders_status',
                    'orders_status_priority',
                ],
                metrics: ['payments_total_revenue'],
                sorts: [], // no sort on the hidden dim
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithPriority);
            expect(result).toBeDefined();
            // orders_status_priority must NOT be in groupByColumns
            // (still hidden from rendered pivot column headers).
            expect(result?.groupByColumns?.some((c) => c.reference === 'orders_status_priority')).toBe(false);
            // sortOnlyDimensions should be empty (the dim isn't a sort target).
            expect(result?.sortOnlyDimensions ?? []).toEqual([]);
            // passthroughDimensions should carry the hidden dim through SQL
            // so cross-field templates can reference it.
            expect(result?.passthroughDimensions).toEqual([
                { reference: 'orders_status_priority' },
            ]);
        });
        it('does not affect non-hidden pivot-column dims', () => {
            // Both pivot dims are visible — neither should go to sortOnlyDimensions
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: {}, // no hidden columns
                    pivotDimensions: [
                        'orders_status',
                        'orders_status_priority',
                    ],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: {
                    columns: ['orders_status', 'orders_status_priority'],
                },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: [
                    'payments_payment_method',
                    'orders_status',
                    'orders_status_priority',
                ],
                metrics: ['payments_total_revenue'],
                sorts: [
                    { fieldId: 'orders_status_priority', descending: false },
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, itemsWithPriority);
            expect(result).toBeDefined();
            // Both dims should be in groupByColumns
            expect(result?.groupByColumns?.some((c) => c.reference === 'orders_status')).toBe(true);
            expect(result?.groupByColumns?.some((c) => c.reference === 'orders_status_priority')).toBe(true);
            // sortOnlyDimensions should be empty (dim is visible)
            expect(result?.sortOnlyDimensions ?? []).toEqual([]);
        });
    });
    describe('sort-only dimensions for Table charts', () => {
        it('puts a hidden helper dimension in sortOnlyColumns instead of indexColumn', () => {
            // orders_status is hidden (visible: false) but used for sort order.
            // payments_payment_method is the pivot dimension (groupByColumn).
            // Result: orders_status must appear in sortOnlyColumns, NOT in indexColumn.
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: {
                        orders_status: { visible: false },
                    },
                    pivotDimensions: ['payments_payment_method'],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: { columns: ['payments_payment_method'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                // Both dims: payments_payment_method (pivot/groupBy) and
                // orders_status (would be row-index but is hidden)
                dimensions: ['payments_payment_method', 'orders_status'],
                metrics: ['payments_total_revenue'],
                sorts: [{ fieldId: 'orders_status', descending: false }],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result).toBeDefined();
            // orders_status must NOT appear in indexColumn
            let indexRefs;
            if (Array.isArray(result?.indexColumn)) {
                indexRefs = result.indexColumn.map((c) => c.reference);
            }
            else if (result?.indexColumn) {
                indexRefs = [result.indexColumn.reference];
            }
            else {
                indexRefs = [];
            }
            expect(indexRefs).not.toContain('orders_status');
            // orders_status MUST appear in sortOnlyColumns
            expect(result?.sortOnlyColumns).toEqual(expect.arrayContaining([
                expect.objectContaining({ reference: 'orders_status' }),
            ]));
        });
        it('does not add a hidden dim to sortOnlyColumns when it is not in sorts', () => {
            // If hidden but not in sorts, it should simply not appear anywhere.
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: {
                        orders_status: { visible: false },
                    },
                    pivotDimensions: ['payments_payment_method'],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: { columns: ['payments_payment_method'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: ['payments_payment_method', 'orders_status'],
                metrics: ['payments_total_revenue'],
                sorts: [
                    {
                        fieldId: 'payments_payment_method',
                        descending: false,
                    },
                ],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result).toBeDefined();
            // sortOnlyColumns should be empty or undefined (no hidden sort dim)
            expect(result?.sortOnlyColumns ?? []).toEqual([]);
        });
        it('routes a hidden row dim with no sort entry to passthroughDimensions (PROD-7873)', () => {
            // Hidden + not sorted row dim used to be dropped from the query
            // entirely, breaking richText / image templates on visible fields
            // that referenced the hidden field via `row.<table>.<field>.raw`.
            // Now it routes through passthroughDimensions: removed from
            // indexColumn rendering, but its values survive `group_by_query`
            // SELECT/GROUP BY so they reach the result rows.
            const chartConfig = {
                type: savedCharts_1.ChartType.TABLE,
                config: {
                    columns: {
                        orders_status: { visible: false },
                    },
                    pivotDimensions: ['payments_payment_method'],
                    showSubtotals: false,
                },
            };
            const savedChart = {
                chartConfig,
                pivotConfig: { columns: ['payments_payment_method'] },
            };
            const mq = {
                ...derivePivotConfigFromChart_mock_1.mockMetricQuery,
                dimensions: ['payments_payment_method', 'orders_status'],
                metrics: ['payments_total_revenue'],
                // No sort on orders_status — purely hidden with no sort role.
                sorts: [],
            };
            const result = (0, derivePivotConfigFromChart_1.derivePivotConfigurationFromChart)(savedChart, mq, derivePivotConfigFromChart_mock_1.mockItems);
            expect(result).toBeDefined();
            // orders_status must NOT appear in indexColumn (still hidden from
            // rendered row labels).
            let indexRefs;
            if (Array.isArray(result?.indexColumn)) {
                indexRefs = result.indexColumn.map((c) => c.reference);
            }
            else if (result?.indexColumn) {
                indexRefs = [result.indexColumn.reference];
            }
            else {
                indexRefs = [];
            }
            expect(indexRefs).not.toContain('orders_status');
            // Not a sort target → not in sortOnlyColumns.
            const sortOnlyRefs = (result?.sortOnlyColumns ?? []).map((c) => c.reference);
            expect(sortOnlyRefs).not.toContain('orders_status');
            expect(result?.sortOnlyColumns ?? []).toEqual([]);
            // passthroughDimensions carries the hidden row dim through SQL
            // so cross-field templates can reference it.
            expect(result?.passthroughDimensions).toEqual([
                { reference: 'orders_status' },
            ]);
        });
    });
});
//# sourceMappingURL=derivePivotConfigFromChart.test.js.map