// (C) 2022-2026 GoodData Corporation
import { factoryNotationFor, insightProperties, isColorFromPalette, isColorMappingItem, isRgbColor, } from "@gooddata/sdk-model";
import { insightConversion, } from "../../utils/embeddingCodeGenerator/insightToPropsConverter/convertor.js";
import { getChartSupportedControls } from "../../utils/propertiesHelper.js";
import { removeUseless } from "../../utils/removeUseless.js";
const supportedChartConfigProperties = new Set([
    "colorMapping",
    "colorPalette",
    "dataLabels",
    "dataPoints",
    "dualAxis",
    "enableChartSorting",
    "enableSeparateTotalLabels",
    "enableJoinedAttributeAxisName",
    "grid",
    "legend",
    "legendLayout",
    "limits",
    "primaryChartType",
    "secondary_xaxis",
    "secondary_yaxis",
    "secondaryChartType",
    "separators",
    "stackMeasures",
    "stackMeasuresToPercent",
    "xaxis",
    "xFormat",
    "xLabel",
    "yaxis",
    "yFormat",
    "yLabel",
    "total",
    "orientation",
    "comparison",
    "cellImageSizing",
    "cellTextWrapping",
    "cellVerticalAlign",
    "rowHeight",
    "inlineVisualizations",
    "hyperLinks",
    "thresholdMeasures",
]);
export function chartConfigFromInsight(insight, ctx) {
    const properties = insightProperties(insight);
    const controls = properties?.["controls"] ?? {};
    const includeColorPalette = ctx?.backend?.capabilities.supportsCustomColorPalettes ?? false;
    const { inlineVisualizations, hyperLinks } = properties;
    const withValuesFromContext = {
        ...{
            inlineVisualizations,
            hyperLinks,
        },
        ...controls,
        ...(ctx?.colorPalette && includeColorPalette ? { colorPalette: ctx.colorPalette } : {}),
        ...(ctx?.settings?.separators ? { separators: ctx.settings.separators } : {}),
        enableChartSorting: true,
        enableSeparateTotalLabels: true,
        enableJoinedAttributeAxisName: true,
    };
    return removeUseless(getChartSupportedControls(Object.fromEntries(Object.entries(withValuesFromContext).filter(([key]) => supportedChartConfigProperties.has(key))), insight));
}
export function chartForecastConfigFromInsight(insight, _ctx) {
    const properties = insightProperties(insight);
    const controls = properties?.["controls"] ?? {};
    if (!controls.forecast || controls.forecast?.enabled === false) {
        return undefined;
    }
    const parsedPeriod = controls.forecast?.period?.toString();
    const forecastPeriod = parsedPeriod ? parseInt(parsedPeriod, 10) : 3;
    const confidenceLevel = controls.forecast?.confidence ?? 0.95;
    const seasonal = controls.forecast?.seasonal;
    return removeUseless({
        confidenceLevel,
        forecastPeriod,
        seasonal,
    });
}
export function chartAdditionalFactories(options) {
    const { getColorMappingPredicatePackage = "@gooddata/sdk-ui-charts" } = options ?? {};
    return [
        {
            importInfo: {
                importType: "named",
                name: "getColorMappingPredicate",
                package: getColorMappingPredicatePackage,
            },
            transformation: (obj) => {
                // sdk-model's isColorMappingItem is intentionally permissive (checks only `id` + `color` presence).
                // That can collide with other objects (e.g. GeoChartNext layer objects have `id` and `color` too).
                // Narrow down to actual IColor payloads to avoid accidental rewrites like `{ predicate, color }`
                // on non-color-mapping objects.
                const isActualColorMappingItem = isColorMappingItem(obj) && (isRgbColor(obj.color) || isColorFromPalette(obj.color));
                return isActualColorMappingItem
                    ? `{predicate: getColorMappingPredicate("${obj.id}"), color: ${factoryNotationFor(obj.color)}}`
                    : undefined;
            },
        },
    ];
}
const chartConfigPropMeta = {
    typeImport: {
        importType: "named",
        name: "IChartConfig",
        package: "@gooddata/sdk-ui-charts",
    },
    cardinality: "scalar",
};
export function chartConfigInsightConversion(propName) {
    return insightConversion(propName, chartConfigPropMeta, chartConfigFromInsight);
}
const chartForecastConfigPropMeta = {
    typeImport: {
        importType: "named",
        name: "IForecastConfig",
        package: "@gooddata/sdk-backend-spi",
    },
    cardinality: "scalar",
};
export function chartForecastConfigInsightConversion(propName) {
    return insightConversion(propName, chartForecastConfigPropMeta, chartForecastConfigFromInsight);
}
//# sourceMappingURL=chartCodeGenUtils.js.map