"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHorizontalBarChartEchartsConfig = void 0;
const getCartesianAxisFormatterConfig_1 = require("../../../../../../visualizations/helpers/getCartesianAxisFormatterConfig");
const formatFieldLabel_1 = require("../../../shared/formatFieldLabel");
const getCommonEChartsConfig_1 = require("../../shared/getCommonEChartsConfig");
/**
 * Generates horizontal bar chart echarts config for server-side rendering
 */
const getHorizontalBarChartEchartsConfig = 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;
    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;
    }
    // Get axis field items for formatting (note: x/y are flipped for horizontal charts)
    const yAxisField = fieldsMap[xDimension]; // Category axis
    const xAxisField = yMetrics[0] ? fieldsMap[yMetrics[0]] : undefined; // Value axis
    const primarySort = sorts?.[0];
    const shouldInverseYAxis = primarySort?.fieldId === xDimension && primarySort?.descending === true;
    return {
        ...(0, getCommonEChartsConfig_1.getCommonEChartsConfig)({
            title: queryTool.title,
            showLegend: metrics.length > 1,
            chartData,
            xAxisLabel: chartConfig?.xAxisLabel,
            yAxisLabel: chartConfig?.yAxisLabel,
        }),
        xAxis: [
            {
                type: 'value',
                ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                    axisItem: xAxisField,
                    show: true,
                }),
            },
        ],
        yAxis: [
            {
                type: chartConfig?.xAxisType ?? 'category',
                ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                    axisItem: yAxisField,
                    show: true,
                }),
                ...(shouldInverseYAxis ? { inverse: true } : {}),
            },
        ],
        series: metrics.map((metric) => ({
            type: 'bar',
            // Use formatted label for non-pivoted metrics, otherwise use the metric name as-is (already formatted by pivot)
            name: yMetrics.includes(metric)
                ? (0, formatFieldLabel_1.formatFieldLabel)(metric, fieldsMap)
                : metric,
            encode: {
                x: metric,
                y: xDimension,
            },
            ...(chartConfig?.stackBars ? { stack: 'stack' } : {}),
        })),
    };
};
exports.getHorizontalBarChartEchartsConfig = getHorizontalBarChartEchartsConfig;
//# sourceMappingURL=horizontalBar.js.map