import { getCartesianAxisFormatterConfig } from '../../../../../../visualizations/helpers/getCartesianAxisFormatterConfig';
import { formatFieldLabel } from '../../../shared/formatFieldLabel';
import { getCommonEChartsConfig } from '../../shared/getCommonEChartsConfig';
/**
 * Generates bar chart echarts config for server-side rendering
 */
export const getBarChartEchartsConfig = 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;
    const yAxisConfig = [
        {
            type: 'value',
            ...getCartesianAxisFormatterConfig({
                axisItem: yAxisField,
                show: true,
            }),
        },
        ...(secondaryYAxisField
            ? [
                {
                    type: 'value',
                    ...getCartesianAxisFormatterConfig({
                        axisItem: secondaryYAxisField,
                        show: true,
                    }),
                },
            ]
            : []),
    ];
    return {
        ...getCommonEChartsConfig({
            title: queryTool.title,
            showLegend: metrics.length > 1,
            chartData,
            xAxisLabel: chartConfig?.xAxisLabel,
            yAxisLabel: chartConfig?.yAxisLabel,
            secondaryYAxisLabel: chartConfig?.secondaryYAxisLabel,
        }),
        xAxis: [
            {
                type: chartConfig?.xAxisType ?? 'category',
                ...getCartesianAxisFormatterConfig({
                    axisItem: xAxisField,
                    show: true,
                }),
                ...(shouldInverseXAxis ? { inverse: true } : {}),
            },
        ],
        yAxis: yAxisConfig,
        series: [
            ...metrics.map((metric) => ({
                type: 'bar',
                yAxisIndex: 0,
                // Use formatted label for non-pivoted metrics, otherwise use the metric name as-is (already formatted by pivot)
                name: yMetrics.includes(metric)
                    ? formatFieldLabel(metric, fieldsMap)
                    : metric,
                encode: {
                    x: xDimension,
                    y: metric,
                },
                ...(chartConfig?.stackBars ? { stack: 'stack' } : {}),
            })),
            ...(secondaryYAxisMetric
                ? [
                    {
                        type: 'bar',
                        yAxisIndex: 1,
                        name: formatFieldLabel(secondaryYAxisMetric, fieldsMap),
                        encode: {
                            x: xDimension,
                            y: secondaryYAxisMetric,
                        },
                        ...(chartConfig?.stackBars ? { stack: 'stack' } : {}),
                    },
                ]
                : []),
        ],
    };
};
//# sourceMappingURL=bar.js.map