"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('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,
                },
            ]);
        });
        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' },
            ]);
        });
    });
});
//# sourceMappingURL=derivePivotConfigFromChart.test.js.map