import * as TSU from "@panyam/tsutils";
import { Space, Group, Syllable } from "./core";
import { Cycle, Bar } from "./cycle";
const ONE = TSU.Num.Fraction.ONE;
export function cycleStrToBarsStrs(cycleStr) {
    return cycleStr
        .replace(/\|+/g, "|")
        .split("|")
        .map((x) => x.trim())
        .filter((x) => x.length > 0);
}
export function parseCycle(cycleStr) {
    return new Cycle({
        bars: cycleStrToBarsStrs(cycleStr).map(parseBar),
    });
}
export function parseBar(barStr) {
    const bars = barStr.replace(/\s+/g, " ").split(" ");
    const cp = new Bar();
    if (bars.length == 1) {
        for (let i = 0; i < barStr.length; i++) {
            let count = 0;
            const ch = barStr[i];
            if (ch == ",") {
                count = 1;
            }
            else {
                const num = parseInt(ch);
                if (num) {
                    count = num;
                }
            }
            while (count > 0) {
                cp.beatLengths.push(ONE);
                count--;
            }
        }
    }
    else {
        for (let i = 0; i < bars.length; i++) {
            const comps = bars[i].split(":");
            const length = comps[0];
            const count = comps[1] || "";
            if (length.length == 0) {
                cp.beatLengths.push(TSU.Num.Frac(1));
            }
            else if (/^,+$/.test(length)) {
                cp.beatLengths.push(TSU.Num.Frac(length.length));
            }
            else {
                cp.beatLengths.push(TSU.Num.Fraction.parse(length));
            }
            const beatCount = parseInt(count);
            if (isNaN(beatCount)) {
                cp.beatCounts.push(1);
            }
            else {
                cp.beatCounts.push(beatCount);
            }
        }
    }
    return cp;
}
export function parseSyllable(value) {
    const notes = [];
    for (let i = 0; i < value.length;) {
        let ch = value[i];
        if (ch == "_") {
            notes.push(new Space(ONE, true));
            i++;
        }
        else if (ch == ",") {
            notes.push(new Space(ONE, false));
            i++;
        }
        else {
            let syll = "";
            while (i < value.length && ch != "," && ch != "_") {
                syll += ch;
                i++;
                ch = value[i];
            }
            notes.push(new Syllable(syll));
        }
    }
    if (notes.length == 1)
        return notes[0];
    return new Group(...notes);
}
export function parseProperty(line) {
    const bars = line.split("=").map((x) => x.trim());
    if (bars.length < 2) {
        throw new Error("Properties must be of type <key> = <value>");
    }
    const key = bars[0];
    const value = bars[1];
    return [key, value];
}
export class SparseArray {
    constructor() {
        this.runs = [];
    }
    get length() {
        let out = 0;
        for (const [, vals] of this.runs) {
            out += vals.length;
        }
        return out;
    }
    valueAt(index, creator) {
        let out = null;
        if (out == null && creator) {
            out = creator();
            this.setAt(index, out);
        }
        return out;
    }
    setAt(index, ...values) {
        return this.splice(index, values.length, ...values);
    }
    removeAt(index, count = 1) {
        return this.splice(index, count);
    }
    splice(index, numToDelete, ...valuesToInsert) {
        return this;
    }
}
//# sourceMappingURL=utils.js.map