"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVerticalBarChartEchartsConfig = void 0;
/**
 * Generates vertical bar chart echarts config for server-side rendering
 */
const getVerticalBarChartEchartsConfig = async (vizTool, rows, fieldsMap, sorts, getPivotedResults) => {
    let chartData = rows;
    let metrics = vizTool.vizConfig.yMetrics;
    if (vizTool.vizConfig.breakdownByDimension) {
        const pivoted = await getPivotedResults(rows, fieldsMap, [vizTool.vizConfig.breakdownByDimension], vizTool.vizConfig.yMetrics, sorts);
        chartData = pivoted.results;
        metrics = pivoted.metrics;
    }
    const fields = Object.keys(chartData[0] || {});
    return {
        dataset: {
            source: chartData,
            dimensions: fields,
        },
        ...(vizTool.title ? { title: { text: vizTool.title } } : {}),
        animation: false,
        backgroundColor: '#fff',
        ...(metrics.length > 1
            ? {
                // This is needed so we don't have overlapping legend and grid
                legend: {
                    top: 40,
                    left: 'left',
                },
                grid: {
                    top: 100,
                },
            }
            : {}),
        xAxis: [
            {
                type: vizTool.vizConfig.xAxisType,
                ...(vizTool.vizConfig.xAxisLabel
                    ? { name: vizTool.vizConfig.xAxisLabel }
                    : {}),
            },
        ],
        yAxis: [
            {
                type: 'value',
                ...(vizTool.vizConfig.yAxisLabel
                    ? { name: vizTool.vizConfig.yAxisLabel }
                    : {}),
            },
        ],
        series: metrics.map((metric) => ({
            type: 'bar',
            name: metric,
            encode: {
                x: vizTool.vizConfig.xDimension,
                y: metric,
            },
            ...(vizTool.vizConfig.stackBars ? { stack: 'stack' } : {}),
        })),
    };
};
exports.getVerticalBarChartEchartsConfig = getVerticalBarChartEchartsConfig;
//# sourceMappingURL=getVerticalBarChartEchartsConfig.js.map