/**
 * Chart helpers — series normalization, color resolution, value formatting.
 * Mirrors the editor app's helper, minus the brand-kit palette path (the
 * presenter is kit-agnostic; `useKitPalette: true` simply falls through to
 * `element.colors`).
 */
export function normalizeSeries(element) {
    if (element.series && element.series.length > 0)
        return element.series;
    return [{ id: 'default', name: '', data: element.data ?? [] }];
}
export function paletteFor(element) {
    return element.colors && element.colors.length > 0 ? element.colors : ['#6366f1'];
}
export function resolveColor(element, seriesIndex, pointIndex, point, series) {
    if (point?.color)
        return point.color;
    if (series?.color)
        return series.color;
    const palette = paletteFor(element);
    const seriesCount = element.series?.length ?? 0;
    const idx = seriesCount > 1 ? seriesIndex : pointIndex;
    return palette[idx % palette.length];
}
export function formatValue(value, fmt) {
    const { prefix = '', suffix = '', decimals = 0, abbreviate = false } = fmt ?? {};
    let n = value;
    let suffixOverride = '';
    if (abbreviate) {
        const abs = Math.abs(n);
        if (abs >= 1_000_000_000) {
            n = n / 1_000_000_000;
            suffixOverride = 'B';
        }
        else if (abs >= 1_000_000) {
            n = n / 1_000_000;
            suffixOverride = 'M';
        }
        else if (abs >= 1_000) {
            n = n / 1_000;
            suffixOverride = 'K';
        }
    }
    const rounded = decimals > 0 ? n.toFixed(decimals) : Math.round(n).toString();
    return `${prefix}${rounded}${suffixOverride}${suffix}`;
}
export function computeYRange(element) {
    const series = normalizeSeries(element);
    let min = Infinity;
    let max = -Infinity;
    if (element.chartType === 'bar' && (element.barLayout ?? 'grouped') === 'stacked' && series.length > 1) {
        const labels = series[0].data.map((d) => d.label);
        for (let i = 0; i < labels.length; i++) {
            let posSum = 0;
            let negSum = 0;
            for (const s of series) {
                const v = s.data[i]?.value ?? 0;
                if (v >= 0)
                    posSum += v;
                else
                    negSum += v;
            }
            if (posSum > max)
                max = posSum;
            if (negSum < min)
                min = negSum;
        }
    }
    else {
        for (const s of series) {
            for (const p of s.data) {
                if (p.value < min)
                    min = p.value;
                if (p.value > max)
                    max = p.value;
            }
        }
    }
    if (!isFinite(min))
        min = 0;
    if (!isFinite(max))
        max = 1;
    if (min > 0)
        min = 0;
    const userMin = element.yAxis?.min;
    const userMax = element.yAxis?.max;
    if (userMin !== undefined)
        min = userMin;
    if (userMax !== undefined)
        max = userMax;
    if (max === min)
        max = min + 1;
    return { min, max };
}
