/**
 * Generates time series chart echarts config for server-side rendering
 */
export const getTimeSeriesChartEchartsConfig = async (vizTool, rows, fieldsMap, sorts, getPivotedResults) => {
    let chartData = rows;
    let metrics = vizTool.vizConfig.yMetrics;
    if (vizTool.vizConfig.breakdownByDimension) {
        // Sort the pivoted data
        const pivoted = await getPivotedResults(rows, fieldsMap, [vizTool.vizConfig.breakdownByDimension], vizTool.vizConfig.yMetrics, sorts);
        chartData = pivoted.results;
        metrics = pivoted.metrics;
    }
    return {
        ...(vizTool.title ? { title: { text: vizTool.title } } : {}),
        ...(metrics.length > 1
            ? {
                // This is needed so we don't have overlapping legend and grid
                legend: {
                    top: 40,
                    left: 'left',
                },
                grid: {
                    top: 100,
                },
            }
            : {}),
        dataset: {
            source: chartData,
            dimensions: Object.keys(chartData[0] || {}),
        },
        animation: false,
        backgroundColor: '#fff',
        xAxis: [
            {
                type: 'time',
                ...(vizTool.vizConfig.xAxisLabel
                    ? { name: vizTool.vizConfig.xAxisLabel }
                    : {}),
            },
        ],
        yAxis: [
            {
                type: 'value',
                ...(vizTool.vizConfig.yAxisLabel
                    ? { name: vizTool.vizConfig.yAxisLabel }
                    : {}),
            },
        ],
        series: metrics.map((metric) => ({
            type: 'line',
            name: metric,
            encode: {
                x: vizTool.vizConfig.xDimension,
                y: metric,
            },
            ...(vizTool.vizConfig.lineType === 'area' && { areaStyle: {} }),
        })),
    };
};
//# sourceMappingURL=getTimeSeriesChartEchartsConfig.js.map