"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getScatterChartEchartsConfig = void 0;
const getCartesianAxisFormatterConfig_1 = require("../../../../../../visualizations/helpers/getCartesianAxisFormatterConfig");
const formatFieldLabel_1 = require("../../../shared/formatFieldLabel");
const getCommonEChartsConfig_1 = require("../../shared/getCommonEChartsConfig");
/**
 * Generates scatter chart echarts config for server-side rendering
 */
const getScatterChartEchartsConfig = 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 we should pivot and have a breakdown dimension, pivot the data
    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
    const xAxisField = fieldsMap[xDimension];
    const yAxisField = yMetrics[0] ? fieldsMap[yMetrics[0]] : undefined;
    const primarySort = sorts?.[0];
    const shouldInverseXAxis = 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,
            useLinePointer: true,
        }),
        xAxis: [
            {
                type: chartConfig?.xAxisType ?? 'category',
                ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                    axisItem: xAxisField,
                    show: true,
                }),
                ...(shouldInverseXAxis ? { inverse: true } : {}),
            },
        ],
        yAxis: [
            {
                type: 'value',
                ...(0, getCartesianAxisFormatterConfig_1.getCartesianAxisFormatterConfig)({
                    axisItem: yAxisField,
                    show: true,
                }),
            },
        ],
        series: metrics.map((metric) => ({
            type: 'scatter',
            // 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: xDimension,
                y: metric,
            },
            showSymbol: true,
        })),
    };
};
exports.getScatterChartEchartsConfig = getScatterChartEchartsConfig;
//# sourceMappingURL=scatter.js.map