import * as TSU from "@panyam/tsutils";
import { Space } from "./";
import { FlatAtom } from "./iterators";
export function ensureUniformSpaces(currOffset, atoms, slotsPerBeat = 1) {
    let lcm = 1;
    let gcd = 0;
    atoms.forEach((a, index) => {
        a.duration = a.duration.factorized;
        const currDen = a.duration.den;
        if (currDen != 1) {
            lcm *= currDen;
            if (gcd == 0) {
                gcd = a.duration.den;
            }
            else {
                gcd = TSU.Num.gcdof(gcd, currDen);
                lcm /= gcd;
            }
        }
    });
    const baseDur = new TSU.Num.Fraction(1, lcm);
    for (let i = 0; i < atoms.length;) {
        const fa = atoms[i];
        const numSpaces = lcm == 1 ? fa.duration.num - 1 : lcm / fa.duration.den - 1;
        fa.duration = baseDur;
        currOffset = currOffset.plus(baseDur);
        i++;
        for (let j = 0; j < numSpaces; j++, i++) {
            const space = new Space(baseDur);
            space.isContinuation = true;
            atoms.splice(i, 0, new FlatAtom(space));
            atoms.splice(i, 0, new FlatAtom(space));
            currOffset = currOffset.plus(baseDur);
        }
    }
}
//# sourceMappingURL=beatutils.js.map