import { isArray, isNullish } from "remeda";
import tinycolor2 from "tinycolor2";
import { InnerRowIndex } from "../../../../../dataReshape/index.js";
const tableStyleMap = {
    backgroundColor: 'bgColor',
    textColor: 'color',
    textFontSize: 'fontSize',
    borderColor: 'borderColor',
    borderLineWidth: 'borderLineWidth',
    barPositiveColor: 'barPositiveColor',
    barNegativeColor: 'barNegativeColor'
};
const interpolateColor = (value, minValue, maxValue, minColor, maxColor)=>{
    const startColor = tinycolor2(minColor).toRgb();
    const endColor = tinycolor2(maxColor).toRgb();
    const normalized = (value - minValue) / (maxValue - minValue);
    const t = Math.max(0, Math.min(1, normalized));
    const r = Math.round(startColor.r + (endColor.r - startColor.r) * t);
    const g = Math.round(startColor.g + (endColor.g - startColor.g) * t);
    const b = Math.round(startColor.b + (endColor.b - startColor.b) * t);
    return `rgb(${r}, ${g}, ${b})`;
};
const pickBodyCellStyle = (bodyCellStyle)=>Object.keys(tableStyleMap).reduce((acc, key)=>{
        if (key in bodyCellStyle) acc[tableStyleMap[key]] = bodyCellStyle[key];
        return acc;
    }, {});
const getColumnMinMax = (allData, field, totalAggregation)=>{
    let min = 1 / 0;
    let max = -1 / 0;
    let sum = 0;
    let count = 0;
    const hasTotal = !!totalAggregation;
    const aggregationType = 'string' == typeof totalAggregation ? totalAggregation : 'sum';
    for (const datum of allData){
        const value = Number(datum[field]);
        if (!Number.isNaN(value)) {
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            count += 1;
        }
    }
    const baseMin = min === 1 / 0 ? 0 : min;
    const baseMax = max === -1 / 0 ? 0 : max;
    if (!hasTotal) return {
        min: baseMin,
        max: baseMax
    };
    const total = (()=>{
        switch(aggregationType){
            case 'avg':
                return count ? sum / count : 0;
            case 'max':
                return baseMax;
            case 'min':
                return baseMin;
            case 'count':
                return count;
            case 'sum':
            default:
                return sum;
        }
    })();
    return {
        min: Math.min(baseMin, total),
        max: Math.max(baseMax, total)
    };
};
const applyColorScale = (value, scale)=>{
    if (!scale) return;
    const numValue = Number(value);
    if (Number.isNaN(numValue) || isNullish(value)) return;
    const minValue = scale.minValue;
    const maxValue = scale.maxValue;
    if (minValue === maxValue) return scale.minColor;
    return interpolateColor(numValue, minValue, maxValue, scale.minColor, scale.maxColor);
};
const getCellOriginalDataByDatum = (datum, hasDynamicFilter, originalDatum)=>{
    const tableInstance = datum?.table;
    let originRowData = tableInstance && hasDynamicFilter ? tableInstance?.getCellOriginRecord(datum?.col, datum?.row) : null;
    if (originRowData && isArray(originRowData)) originRowData = originRowData[0];
    return originRowData ? {
        ...originalDatum,
        [InnerRowIndex]: originRowData?.[InnerRowIndex]
    } : null;
};
export { applyColorScale, getCellOriginalDataByDatum, getColumnMinMax, interpolateColor, pickBodyCellStyle };

//# sourceMappingURL=common.js.map