"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const savedCharts_1 = require("./savedCharts");
const pivotedSeries = (field, pivotField, pivotValue, overrides = {}) => ({
    type: savedCharts_1.CartesianSeriesType.BAR,
    stack: 'default',
    isFilteredOut: true,
    encode: {
        xRef: { field: 'orders_created_date' },
        yRef: {
            field,
            pivotValues: [{ field: pivotField, value: pivotValue }],
        },
        x: 'orders_created_date',
        y: `${field}.${pivotField}.${pivotValue}`,
    },
    ...overrides,
});
const cartesianConfig = (series) => ({
    type: savedCharts_1.ChartType.CARTESIAN,
    config: {
        layout: {
            xField: 'orders_created_date',
            yField: ['orders_count'],
        },
        eChartsConfig: {
            series,
        },
    },
});
describe('removePivotedSeriesValuesFromChartConfig', () => {
    it('strips data-level pivot values and dedupes pivoted series', () => {
        const result = (0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(cartesianConfig([
            pivotedSeries('orders_count', 'orders_status', 'completed', {
                name: 'Completed orders',
                color: '#1f77b4',
            }),
            pivotedSeries('orders_count', 'orders_status', 'returned', {
                name: 'Returned orders',
                color: '#d62728',
                isFilteredOut: false,
            }),
        ]));
        expect(result.type).toBe(savedCharts_1.ChartType.CARTESIAN);
        const series = result.config?.eChartsConfig
            .series;
        expect(series).toHaveLength(1);
        expect(series?.[0]).toEqual(expect.objectContaining({
            type: savedCharts_1.CartesianSeriesType.BAR,
            stack: 'default',
            isFilteredOut: true,
        }));
        expect(series?.[0].name).toBeUndefined();
        expect(series?.[0].color).toBeUndefined();
        expect(series?.[0].encode).toEqual({
            xRef: { field: 'orders_created_date' },
            yRef: { field: 'orders_count' },
        });
    });
    it('keeps multiple metrics separate', () => {
        const result = (0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(cartesianConfig([
            pivotedSeries('orders_count', 'orders_status', 'completed'),
            pivotedSeries('orders_total', 'orders_status', 'completed'),
        ]));
        const series = result.config?.eChartsConfig
            .series;
        expect(series).toHaveLength(2);
        expect(series?.map((s) => s.encode.yRef.field)).toEqual([
            'orders_count',
            'orders_total',
        ]);
    });
    it('leaves non-pivoted series customizations untouched', () => {
        const nonPivoted = pivotedSeries('orders_count', 'orders_status', 'completed', {
            color: '#2ca02c',
            name: 'Orders count',
            encode: {
                xRef: { field: 'orders_created_date' },
                yRef: { field: 'orders_count' },
                x: 'orders_created_date',
                y: 'orders_count',
            },
        });
        const result = (0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(cartesianConfig([nonPivoted]));
        const series = result.config?.eChartsConfig
            .series;
        expect(series).toEqual([nonPivoted]);
    });
    it('returns non-cartesian configs unchanged', () => {
        const config = {
            type: savedCharts_1.ChartType.TABLE,
            config: { columns: {} },
        };
        expect((0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(config)).toBe(config);
    });
    it('handles missing or empty series', () => {
        const missingSeries = {
            type: savedCharts_1.ChartType.CARTESIAN,
            config: {
                layout: {},
                eChartsConfig: {},
            },
        };
        const emptySeries = cartesianConfig([]);
        expect((0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(missingSeries)).toBe(missingSeries);
        expect((0, savedCharts_1.removePivotedSeriesValuesFromChartConfig)(emptySeries)).toBe(emptySeries);
    });
});
describe('ChartType.DATA_APP_VIZ variant', () => {
    const dataAppVizConfig = {
        type: savedCharts_1.ChartType.DATA_APP_VIZ,
        config: {
            dataAppVizUuid: 'data-app-viz-1',
            fieldMapping: { category: 'orders_status', value: 'orders_count' },
        },
    };
    it('round-trips through getChartKind / getChartType without loss', () => {
        const kind = (0, savedCharts_1.getChartKind)(dataAppVizConfig.type, dataAppVizConfig.config);
        expect(kind).toBe(savedCharts_1.ChartKind.DATA_APP_VIZ);
        expect((0, savedCharts_1.getChartType)(kind)).toBe(savedCharts_1.ChartType.DATA_APP_VIZ);
    });
    it('isDataAppVizChart identifies the config and rejects others', () => {
        expect((0, savedCharts_1.isDataAppVizChart)(dataAppVizConfig.config)).toBe(true);
        expect((0, savedCharts_1.isDataAppVizChart)({ spec: {} })).toBe(false);
        expect((0, savedCharts_1.isDataAppVizChart)(undefined)).toBe(false);
    });
    it('is not misclassified as a table config', () => {
        expect((0, savedCharts_1.isTableChartConfig)(dataAppVizConfig.config)).toBe(false);
    });
});
//# sourceMappingURL=savedCharts.test.js.map