import { assert } from "../../assert.js";
import { clamp } from "../clamp.js";
import { computeStatisticalPartialMedian } from "./computeStatisticalPartialMedian.js";

/**
 *
 * @param {number[]} values
 * @param {number} percentile_low 0..1
 * @param {number} percentile_high 0..1
 * @returns {number}
 */
export function computeStatisticalPercentile(values, percentile_low, percentile_high) {
    assert.isNumber(percentile_low, 'percentile_low');
    assert.notNaN(percentile_low, 'percentile_low');

    assert.isNumber(percentile_high, 'percentile_high');
    assert.notNaN(percentile_high, 'percentile_high');

    assert.greaterThanOrEqual(percentile_high, percentile_low);

    const n = values.length;

    const start = clamp(Math.floor(n * percentile_low), 0, n - 1);
    const end = clamp(Math.ceil(n * percentile_high), 0, n - 1);

    return computeStatisticalPartialMedian(values, start, end);
}
