"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.Repeat = exports.ScopedGroup = exports.Section = exports.SetBeatDuration = exports.SetCycle = exports.SetBreaks = exports.ApplyLayout = exports.LayoutParamCommand = exports.CreateRole = exports.CreateLine = exports.AddAtoms = exports.ActivateRole = exports.MetaData = exports.RawEmbedding = void 0;
const TSU = __importStar(require("@panyam/tsutils"));
const utils_1 = require("./utils");
const notation_1 = require("./notation");
class RawEmbedding extends notation_1.Command {
    get rawContents() {
        return this.getParamAt(0);
    }
    applyToBlock(container) {
        const raw = new notation_1.RawBlock(this.rawContents);
        container.addBlockItem(raw);
    }
}
exports.RawEmbedding = RawEmbedding;
class MetaData extends notation_1.Command {
    applyToBlock(container) {
        if (container instanceof notation_1.Notation) {
            container.addMetaData(this.meta);
        }
    }
    get meta() {
        const out = new notation_1.MetaData(this.key, this.value);
        return out;
    }
    get key() {
        return this.getParamAt(0);
    }
    get value() {
        return this.getParamAt(1);
    }
}
exports.MetaData = MetaData;
class ActivateRole extends notation_1.Command {
    applyToBlock(container) {
        container.setCurrRole(this.roleName);
    }
    get roleName() {
        return this.getParamAt(0);
    }
}
exports.ActivateRole = ActivateRole;
class AddAtoms extends notation_1.Command {
    constructor(...atoms) {
        super();
        this.atoms = atoms;
        this.index = 0;
    }
    debugValue() {
        return {
            name: this.name,
            index: this.index,
            atoms: this.atoms.map((a) => a.debugValue()),
        };
    }
    applyToBlock(container) {
        let roleDef = container.currRoleDef;
        if (roleDef == null) {
            roleDef = container.newRoleDef("Sw", true);
        }
        const line = container.currentLine;
        if (container instanceof notation_1.Notation) {
            const lpForLine = line.layoutParams;
            if (lpForLine == null) {
                line.layoutParams = container.layoutParams;
            }
            else {
                TSU.assert(lpForLine == container.layoutParams, "Layout parameters have changed so a new line should have been started");
            }
        }
        line.addAtoms(roleDef.name, roleDef.notesOnly, ...this.atoms);
    }
}
exports.AddAtoms = AddAtoms;
class CreateLine extends notation_1.Command {
    applyToBlock(container) {
        const line = container.newLine();
        line.offset = this.offset;
        line.marginText = this.marginText;
    }
    get offset() {
        let offset = this.getParam("offset") || TSU.Num.Fraction.ZERO;
        if (typeof offset === "number")
            offset = new TSU.Num.Fraction(offset);
        return offset;
    }
    get marginText() {
        if (this.params.length > 0) {
            if (this.params[0].key == null && typeof this.params[0].value === "string") {
                return this.params[0].value.trim();
            }
        }
        return this.getParam("marginText") || "";
    }
}
exports.CreateLine = CreateLine;
class CreateRole extends notation_1.Command {
    applyToBlock(container) {
        const name = this.getParamAt(0);
        container.newRoleDef(name, this.notesOnly);
    }
    get notesOnly() {
        const notesOnly = this.getParam("notes");
        return notesOnly == "true" || notesOnly == "yes" || notesOnly == true;
    }
}
exports.CreateRole = CreateRole;
class LayoutParamCommand extends notation_1.Command {
}
exports.LayoutParamCommand = LayoutParamCommand;
class ApplyLayout extends notation_1.Command {
    validateParams() {
        if (this.params.length != 1 || typeof this.params[0].value !== "string") {
            throw new Error("layout command must contain one string argument");
        }
    }
    applyToBlock(container) {
        if (container instanceof notation_1.Notation) {
            const value = this.params[0].value;
            container.ensureNamedLayoutParams(value);
        }
    }
}
exports.ApplyLayout = ApplyLayout;
class SetBreaks extends LayoutParamCommand {
    get pattern() {
        return this.params.map((cmd) => cmd.value);
    }
    validateParams() {
        for (const param of this.params) {
            if (param.key != null) {
                throw new Error("Breaks command cannot have keyword params");
            }
            if (typeof param.value !== "number") {
                throw new Error("Breaks command must be a list of integers");
            }
        }
    }
    applyToBlock(container) {
        container.localBreaks = this.pattern;
        if (container instanceof notation_1.Notation) {
            container.resetLayoutParams();
        }
    }
}
exports.SetBreaks = SetBreaks;
class SetCycle extends LayoutParamCommand {
    applyToBlock(container) {
        const value = this.params[0].value;
        const cycle = (0, utils_1.parseCycle)(value);
        container.localCycle = cycle;
        if (container instanceof notation_1.Notation) {
            container.resetLayoutParams();
        }
    }
}
exports.SetCycle = SetCycle;
class SetBeatDuration extends LayoutParamCommand {
    validateParams() {
        if (this.params.length != 1 || typeof this.params[0].value !== "number") {
            throw new Error("beatDuration command must contain one number");
        }
    }
    get beatDuration() {
        return this.params[0].value;
    }
    applyToBlock(container) {
        container.localAtomsPerBeat = this.beatDuration;
        if (container instanceof notation_1.Notation) {
            container.resetLayoutParams();
        }
    }
}
exports.SetBeatDuration = SetBeatDuration;
class Section extends notation_1.Command {
    get sectionName() {
        return this.getParamAt(0) || "";
    }
    validateParams() {
        if (this.params.length < 1 || typeof this.params[0].value !== "string") {
            throw new Error("section command must contain a string name");
        }
    }
    applyToBlock(_container) {
    }
}
exports.Section = Section;
class ScopedGroup extends notation_1.Command {
    get groupName() {
        return this.getParamAt(0) || "";
    }
    applyToBlock(_container) {
    }
}
exports.ScopedGroup = ScopedGroup;
class Repeat extends notation_1.Command {
    get count() {
        const count = this.getParamAt(0);
        if (typeof count === "number") {
            return count;
        }
        return 0;
    }
    validateParams() {
        if (this.params.length > 0) {
            const count = this.params[0].value;
            if (typeof count !== "number" || count < 0) {
                throw new Error("repeat command count must be a non-negative number");
            }
        }
    }
    applyToBlock(_container) {
    }
}
exports.Repeat = Repeat;
//# sourceMappingURL=commands.js.map