import { xBoxPlotWithOutliers } from "./xBoxPlotWithOutliers.js";
import { xMean } from "./xMean.js";
import { xStandardDeviation } from "./xStandardDeviation.js";
/**
 * Calculate distribution statistics of an array without providing options. Statistics
 * like sd and mean are calculated after removing outliers.
 * This ensure that the statistics are calculated in the same way in all the packages
 * If the array is empty it will throw an error
 * If the array has a length of 1, sd will be NaN (unbiased calculation of sd)
 * @param array - data.
 * @returns q1, median, q3, min, max.
 */
export function xRobustDistributionStats(array) {
    const boxPlot = xBoxPlotWithOutliers(array);
    let filteredArray;
    if (boxPlot.max - boxPlot.min <= Number.EPSILON) {
        return {
            ...boxPlot,
            mean: boxPlot.median,
            sd: array.length === 1 ? Number.NaN : 0,
            nb: array.length,
        };
    }
    if (boxPlot.outliers.length === 0) {
        filteredArray = array;
    }
    else {
        filteredArray = new Float64Array(array.length - boxPlot.outliers.length);
        let j = 0;
        for (const element of array) {
            if (element >= boxPlot.min && element <= boxPlot.max) {
                filteredArray[j++] = element;
            }
        }
    }
    return {
        ...boxPlot,
        mean: xMean(filteredArray),
        sd: xStandardDeviation(filteredArray),
        nb: filteredArray.length,
    };
}
//# sourceMappingURL=xRobustDistributionStats.js.map