import { CartesianSeriesType, ChartType, } from '../../../../../../types/savedCharts';
import { formatFieldLabel } from '../../../shared/formatFieldLabel';
export const getBarChartConfig = ({ 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: ChartType.CARTESIAN,
        config: {
            layout: {
                xField: xDimension,
                yField: secondaryYAxisMetric
                    ? [
                        ...(chartConfig?.yAxisMetrics || metricQuery.metrics),
                        secondaryYAxisMetric,
                    ]
                    : chartConfig?.yAxisMetrics || metricQuery.metrics,
                stack: !!chartConfig?.stackBars,
            },
            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: CartesianSeriesType.BAR,
                        yAxisIndex: 0,
                        ...(chartConfig?.stackBars && {
                            stack: metric,
                        }),
                        encode: {
                            xRef: { field: xDimension },
                            yRef: { field: metric },
                        },
                        name: formatFieldLabel(metric, fieldsMap),
                    })),
                    ...(secondaryYAxisMetric
                        ? [
                            {
                                type: CartesianSeriesType.BAR,
                                yAxisIndex: 1,
                                ...(chartConfig?.stackBars && {
                                    stack: secondaryYAxisMetric,
                                }),
                                encode: {
                                    xRef: { field: xDimension },
                                    yRef: { field: secondaryYAxisMetric },
                                },
                                name: formatFieldLabel(secondaryYAxisMetric, fieldsMap),
                            },
                        ]
                        : []),
                ],
            },
        },
    };
};
//# sourceMappingURL=bar.js.map