import { ColorEncoding, ColorIdEncoding, ORIGINAL_DATA } from "./constant.js";
import { omit } from "remeda";
const foldMeasures = (dataset, measures, options)=>{
    const { measureId, measureName, measureValue, colorMeasureId, allowEmptyFold = true, omitIds = [] } = options || {};
    const foldInfo = {
        measureId,
        measureName,
        measureValue,
        statistics: {
            max: -1 / 0,
            min: 1 / 0,
            sum: 0,
            count: 0,
            colorMin: 1 / 0,
            colorMax: -1 / 0
        },
        foldMap: {}
    };
    if (!allowEmptyFold && 0 === measures.length) return {
        dataset,
        foldInfo
    };
    const result = new Array(dataset.length * measures.length);
    let index = 0;
    for(let i = 0; i < dataset.length; i++)for(let j = 0; j < measures.length; j++){
        const datum = omit({
            ...dataset[i]
        }, omitIds);
        datum[ORIGINAL_DATA] = dataset[i];
        const measure = measures[j];
        const { id, alias } = measure;
        datum[id] = dataset[i][id];
        datum[measureId] = id;
        datum[measureName] = alias || id;
        datum[measureValue] = dataset[i][id];
        const valueNumber = Number(datum[id]);
        foldInfo.statistics.min = Math.min(foldInfo.statistics.min, valueNumber);
        foldInfo.statistics.max = Math.max(foldInfo.statistics.max, valueNumber);
        foldInfo.statistics.sum += valueNumber;
        foldInfo.statistics.count++;
        if ('color' === measure.encoding) {
            foldInfo.statistics.colorMin = foldInfo.statistics.min;
            foldInfo.statistics.colorMax = foldInfo.statistics.max;
            datum[ColorEncoding] = valueNumber;
            datum[ColorIdEncoding] = measure.id;
        } else if (colorMeasureId) {
            const value = datum[ORIGINAL_DATA][colorMeasureId];
            datum[ColorEncoding] = value;
            datum[ColorIdEncoding] = colorMeasureId;
            const valueNumber = Number(value);
            foldInfo.statistics.colorMin = Math.min(foldInfo.statistics.colorMin, valueNumber);
            foldInfo.statistics.colorMax = Math.max(foldInfo.statistics.colorMax, valueNumber);
        }
        foldInfo.foldMap[id] = alias;
        result[index++] = datum;
    }
    return {
        dataset: result,
        foldInfo
    };
};
export { foldMeasures };

//# sourceMappingURL=foldMeasures.js.map