"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeReplaceFieldsInChartVersion = maybeReplaceFieldsInChartVersion;
exports.getCustomLabelsFromVizTableConfig = getCustomLabelsFromVizTableConfig;
exports.getHiddenFieldsFromVizTableConfig = getHiddenFieldsFromVizTableConfig;
exports.getColumnOrderFromVizTableConfig = getColumnOrderFromVizTableConfig;
const item_1 = require("./item");
function maybeReplaceFieldsInChartVersion({ fieldsToReplace, chartVersion, }) {
    let hasChanges = false;
    const skippedFields = {
        customMetrics: {},
    };
    const newChartData = { ...chartVersion };
    // Replace custom metrics
    const customMetricsToReplace = Object.entries(fieldsToReplace.customMetrics);
    customMetricsToReplace.forEach(([customMetricToReplace, replaceWith]) => {
        if (customMetricToReplace === replaceWith.replaceWithFieldId) {
            const foundCustomMetric = !!chartVersion.metricQuery.additionalMetrics?.find((additionalMetric) => (0, item_1.getItemId)(additionalMetric) === customMetricToReplace);
            if (foundCustomMetric) {
                hasChanges = true;
                // remove custom metric
                newChartData.metricQuery.additionalMetrics =
                    newChartData.metricQuery.additionalMetrics?.filter((additionalMetric) => (0, item_1.getItemId)(additionalMetric) !==
                        customMetricToReplace);
            }
            else {
                skippedFields.customMetrics[customMetricToReplace] = {
                    reason: `Custom metric ${customMetricToReplace} not found in chart version.`,
                    replaceWithFieldId: replaceWith.replaceWithFieldId,
                };
            }
        }
        else {
            skippedFields.customMetrics[customMetricToReplace] = {
                reason: 'Replacing custom metrics with a metric with a different ID is not supported yet.',
                replaceWithFieldId: replaceWith.replaceWithFieldId,
            };
        }
    });
    return {
        hasChanges,
        skippedFields,
        chartVersion: newChartData || chartVersion,
    };
}
/**
 * Extracts custom labels from a VizTableConfig columns configuration.
 * Returns a record mapping field references to their custom labels,
 * but only for fields where the label differs from the reference.
 */
function getCustomLabelsFromVizTableConfig(config) {
    if (!config?.columns)
        return {};
    return Object.fromEntries(Object.entries(config.columns)
        .filter(([_, columnConfig]) => columnConfig.label !== columnConfig.reference)
        .map(([key, columnConfig]) => [key, columnConfig.label]));
}
/**
 * Extracts hidden field references from a VizTableConfig columns configuration.
 * Returns an array of field references that are marked as not visible.
 */
function getHiddenFieldsFromVizTableConfig(config) {
    if (!config?.columns)
        return [];
    return Object.entries(config.columns)
        .filter(([_, columnConfig]) => !columnConfig.visible)
        .map(([key]) => key);
}
/**
 * Extracts column order from a VizTableConfig columns configuration.
 * Returns an array of field references sorted by their order property.
 */
function getColumnOrderFromVizTableConfig(config) {
    if (!config?.columns)
        return [];
    // If none of the columns have an empty property, return an empty list
    // for sql queries, we will order the columns based on the results
    if (Object.values(config.columns).every((column) => column.order === undefined)) {
        return [];
    }
    return Object.entries(config.columns)
        .sort(([_, a], [__, b]) => (a.order ?? 0) - (b.order ?? 0))
        .map(([key]) => key);
}
//# sourceMappingURL=charts.js.map