import { xyObjectMaxXPoint } from "./xyObjectMaxXPoint.js";
import { xyObjectMinXPoint } from "./xyObjectMinXPoint.js";
/**
 * Filter the array by taking the higher points (max y value) and only.
 * Keep one per slot. There are 2 different slots, the smallest one will have the
 * new property `close` to true
 * @param points - array of all the points
 * @param options - options.
 * @returns copy of points with 'close' property.
 */
export function xyObjectBestPoints(points, options = {}) {
    const { from = xyObjectMinXPoint(points).x, to = xyObjectMaxXPoint(points).x, limit = 20, threshold = 0.01, numberCloseSlots = 50, numberSlots = 10, } = options;
    const slot = (to - from) / numberSlots;
    const closeSlot = (to - from) / numberCloseSlots;
    const selected = points
        .filter((point) => point.x >= from && point.x <= to)
        .map((point) => {
        return {
            point,
        };
    });
    selected.sort((a, b) => {
        return b.point.y - a.point.y;
    });
    const toReturn = [];
    if (selected.length === 0)
        return [];
    const minY = selected[0].point.y * threshold;
    peakLoop: for (const item of selected) {
        if (item.point.y < minY) {
            break;
        }
        let close = false;
        for (const existing of toReturn) {
            if (Math.abs(existing.x - item.point.x) < closeSlot) {
                continue peakLoop;
            }
            if (Math.abs(existing.x - item.point.x) < slot) {
                close = true;
            }
        }
        const newPeak = { ...item.point, close };
        toReturn.push(newPeak);
        if (toReturn.length === limit)
            break;
    }
    toReturn.sort((a, b) => a.x - b.x);
    return toReturn;
}
//# sourceMappingURL=xyObjectBestPoints.js.map