"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const savedCharts_1 = require("../types/savedCharts");
const types_1 = require("../visualizations/types");
const derivePivotConfigFromChart_1 = require("./derivePivotConfigFromChart");
const derivePivotConfigFromChart_mock_1 = require("./derivePivotConfigFromChart.mock");
// Jest provides describe/it/expect globals
const field_1 = require("../types/field");
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,
                },
            ],
        });
    });
    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,
                },
            ]);
        });
    });
});
//# sourceMappingURL=derivePivotConfigFromChart.test.js.map