import { xyJoinX } from "../xy/index.js";
import { getSlots } from "./utils/getSlots.js";
/**
 * Merge DataXY
 * We have an array of DataXY and the goal is to merge all the values that are the closest possible
 * @param data - data
 * @param options - options.
 */
export function xyArrayMerge(data, options = {}) {
    const { delta = 1 } = options;
    // We start by checking that the data/spectra don't have peaks too close and we simplify them.
    data = data.map((spectrum) => xyJoinX(spectrum, { delta }));
    // At first, we will calculate the X values (simple mean).
    const slots = getSlots(data, { delta });
    const x = Float64Array.from(slots.map((slot) => slot.average));
    const y = new Float64Array(x.length);
    const positions = new Uint32Array(data.length);
    for (let i = 0; i < slots.length; i++) {
        const slot = slots[i];
        for (let j = 0; j < data.length; j++) {
            const spectrum = data[j];
            while (positions[j] < spectrum.x.length &&
                spectrum.x[positions[j]] <= slot.to) {
                y[i] += spectrum.y[positions[j]];
                positions[j]++;
            }
        }
    }
    return { x, y };
}
//# sourceMappingURL=xyArrayMerge.js.map