"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLineChartEchartsConfig = void 0;
const getCartesianAxisFormatterConfig_1 = require("../../../../../../visualizations/helpers/getCartesianAxisFormatterConfig");
const formatFieldLabel_1 = require("../../../shared/formatFieldLabel");
const getCommonEChartsConfig_1 = require("../../shared/getCommonEChartsConfig");
/**
 * Generates line chart echarts config for server-side rendering
 */
const getLineChartEchartsConfig = async (queryTool, rows, fieldsMap, getPivotedResults) => {
    const { dimensions, metrics: queryMetrics, sorts } = queryTool.queryConfig;
    const { chartConfig } = queryTool;
    const xDimension = chartConfig?.xAxisDimension || dimensions[0];
    const yMetrics = chartConfig?.yAxisMetrics || queryMetrics;
    const secondaryYAxisMetric = chartConfig?.secondaryYAxisMetric;
    let metrics = yMetrics;
    let chartData = rows;
    if (chartConfig?.groupBy?.length) {
        const pivoted = await getPivotedResults(rows, fieldsMap, chartConfig.groupBy, yMetrics, sorts);
        chartData = pivoted.results;
        metrics = pivoted.metrics;
    }
    const xAxisField = fieldsMap[xDimension];
    const yAxisField = yMetrics[0] ? fieldsMap[yMetrics[0]] : undefined;
    const secondaryYAxisField = secondaryYAxisMetric
        ? fieldsMap[secondaryYAxisMetric]
        : undefined;
    const primarySort = sorts?.[0];
    const shouldInverseXAxis = primarySort?.fieldId === xDimension && primarySort?.descending === true;
    // Build yAxis array based on whether secondary axis is specified
    const yAxisConfig = [
        {
            type: 'value',
            ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                axisItem: yAxisField,
                show: true,
            }),
        },
        ...(secondaryYAxisField
            ? [
                {
                    type: 'value',
                    ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                        axisItem: secondaryYAxisField,
                        show: true,
                    }),
                },
            ]
            : []),
    ];
    return {
        ...(0, getCommonEChartsConfig_1.getCommonEChartsConfig)({
            title: queryTool.title,
            showLegend: metrics.length > 1,
            chartData,
            xAxisLabel: chartConfig?.xAxisLabel,
            yAxisLabel: chartConfig?.yAxisLabel,
            secondaryYAxisLabel: chartConfig?.secondaryYAxisLabel,
            useLinePointer: true,
        }),
        xAxis: [
            {
                type: chartConfig?.xAxisType ?? 'time',
                ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                    axisItem: xAxisField,
                    show: true,
                }),
                ...(shouldInverseXAxis ? { inverse: true } : {}),
            },
        ],
        yAxis: yAxisConfig,
        series: [
            ...metrics.map((metric) => ({
                type: 'line',
                yAxisIndex: 0,
                // Use formatted label for non-pivoted metrics, otherwise use the metric name as-is (already formatted by pivot)
                name: yMetrics.includes(metric)
                    ? (0, formatFieldLabel_1.formatFieldLabel)(metric, fieldsMap)
                    : metric,
                encode: {
                    x: xDimension,
                    y: metric,
                },
                ...(chartConfig?.lineType === 'area' && {
                    areaStyle: {},
                }),
                showSymbol: true,
            })),
            ...(secondaryYAxisMetric
                ? [
                    {
                        type: 'line',
                        yAxisIndex: 1,
                        name: (0, formatFieldLabel_1.formatFieldLabel)(secondaryYAxisMetric, fieldsMap),
                        encode: {
                            x: xDimension,
                            y: secondaryYAxisMetric,
                        },
                        ...(chartConfig?.lineType === 'area' && {
                            areaStyle: {},
                        }),
                        showSymbol: true,
                    },
                ]
                : []),
        ],
    };
};
exports.getLineChartEchartsConfig = getLineChartEchartsConfig;
//# sourceMappingURL=line.js.map