"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLineChartConfig = void 0;
const savedCharts_1 = require("../../../../../../types/savedCharts");
const formatFieldLabel_1 = require("../../../shared/formatFieldLabel");
const getLineChartConfig = ({ queryTool, metricQuery, fieldsMap, chartConfig, metadata, }) => {
    const { dimensions, metrics } = queryTool.queryConfig;
    const xDimension = chartConfig?.xAxisDimension || dimensions[0];
    const yMetrics = chartConfig?.yAxisMetrics || metrics;
    const secondaryYAxisMetric = chartConfig?.secondaryYAxisMetric;
    // Build yAxis array based on whether secondary axis is specified
    const yAxisConfig = secondaryYAxisMetric
        ? [
            {
                ...(chartConfig?.yAxisLabel
                    ? { name: chartConfig.yAxisLabel }
                    : {}),
            },
            {
                ...(chartConfig?.secondaryYAxisLabel
                    ? { name: chartConfig.secondaryYAxisLabel }
                    : {}),
            },
        ]
        : [
            {
                ...(chartConfig?.yAxisLabel
                    ? { name: chartConfig.yAxisLabel }
                    : {}),
            },
        ];
    return {
        type: savedCharts_1.ChartType.CARTESIAN,
        config: {
            layout: {
                xField: xDimension,
                yField: secondaryYAxisMetric
                    ? [
                        ...(chartConfig?.yAxisMetrics || metricQuery.metrics),
                        secondaryYAxisMetric,
                    ]
                    : chartConfig?.yAxisMetrics || metricQuery.metrics,
            },
            eChartsConfig: {
                ...(metadata.title ? { title: { text: metadata.title } } : {}),
                legend: {
                    show: true,
                    type: 'scroll',
                },
                grid: { containLabel: true },
                xAxis: [
                    {
                        ...(chartConfig?.xAxisLabel
                            ? { name: chartConfig.xAxisLabel }
                            : {}),
                    },
                ],
                yAxis: yAxisConfig,
                series: [
                    ...yMetrics.map((metric) => ({
                        type: savedCharts_1.CartesianSeriesType.LINE,
                        yAxisIndex: 0,
                        ...(chartConfig?.lineType === 'area' && {
                            areaStyle: {},
                        }),
                        name: (0, formatFieldLabel_1.formatFieldLabel)(metric, fieldsMap),
                        encode: {
                            xRef: { field: xDimension },
                            yRef: { field: metric },
                        },
                        stack: chartConfig?.lineType === 'area'
                            ? 'total'
                            : undefined,
                    })),
                    ...(secondaryYAxisMetric
                        ? [
                            {
                                type: savedCharts_1.CartesianSeriesType.LINE,
                                yAxisIndex: 1,
                                ...(chartConfig?.lineType === 'area' && {
                                    areaStyle: {},
                                }),
                                name: (0, formatFieldLabel_1.formatFieldLabel)(secondaryYAxisMetric, fieldsMap),
                                encode: {
                                    xRef: { field: xDimension },
                                    yRef: { field: secondaryYAxisMetric },
                                },
                                stack: chartConfig?.lineType === 'area'
                                    ? 'total'
                                    : undefined,
                            },
                        ]
                        : []),
                ],
            },
        },
    };
};
exports.getLineChartConfig = getLineChartConfig;
//# sourceMappingURL=line.js.map