"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SparseArray = void 0;
exports.cycleStrToBarsStrs = cycleStrToBarsStrs;
exports.parseCycle = parseCycle;
exports.parseBar = parseBar;
exports.parseSyllable = parseSyllable;
exports.parseProperty = parseProperty;
const TSU = __importStar(require("@panyam/tsutils"));
const core_1 = require("./core");
const cycle_1 = require("./cycle");
const ONE = TSU.Num.Fraction.ONE;
function cycleStrToBarsStrs(cycleStr) {
    return cycleStr
        .replace(/\|+/g, "|")
        .split("|")
        .map((x) => x.trim())
        .filter((x) => x.length > 0);
}
function parseCycle(cycleStr) {
    return new cycle_1.Cycle({
        bars: cycleStrToBarsStrs(cycleStr).map(parseBar),
    });
}
function parseBar(barStr) {
    const bars = barStr.replace(/\s+/g, " ").split(" ");
    const cp = new cycle_1.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;
}
function parseSyllable(value) {
    const notes = [];
    for (let i = 0; i < value.length;) {
        let ch = value[i];
        if (ch == "_") {
            notes.push(new core_1.Space(ONE, true));
            i++;
        }
        else if (ch == ",") {
            notes.push(new core_1.Space(ONE, false));
            i++;
        }
        else {
            let syll = "";
            while (i < value.length && ch != "," && ch != "_") {
                syll += ch;
                i++;
                ch = value[i];
            }
            notes.push(new core_1.Syllable(syll));
        }
    }
    if (notes.length == 1)
        return notes[0];
    return new core_1.Group(...notes);
}
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];
}
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;
    }
}
exports.SparseArray = SparseArray;
//# sourceMappingURL=utils.js.map