"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.BeatView = exports.MarkerView = void 0;
const TSU = __importStar(require("@panyam/tsutils"));
const shapes_1 = require("../shapes");
const atomviews_1 = require("./atomviews");
const beatview_1 = require("../beatview");
const embelishments_1 = require("./embelishments");
class MarkerView extends shapes_1.ElementShape {
    constructor(cell, beat, markers, isPreMarker, rootElement, config) {
        const rootGroup = TSU.DOM.createSVGNode("g", {
            parent: rootElement,
            attrs: {
                class: "markerView",
                pre: isPreMarker,
                roleName: beat.role.name,
                beatIndex: "" + beat.index,
                gridRow: cell.rowIndex,
                gridCol: cell.colIndex,
            },
        });
        super(rootGroup);
        this.cell = cell;
        this.beat = beat;
        this.markers = markers;
        this.isPreMarker = isPreMarker;
        this.rootElement = rootElement;
        this.needsLayout = true;
        this.rootGroup = rootGroup;
        this.textElement = TSU.DOM.createSVGNode("text", {
            parent: rootGroup,
            attrs: {
                class: "markerText",
                pre: isPreMarker,
                dx: isPreMarker ? 0 : 15,
            },
            text: this.markers[0].text,
        });
    }
    refreshMinSize() {
        const ts = TSU.DOM.svgBBox(this.textElement);
        const totalWidth = ts.width;
        const maxHeight = ts.height;
        return new TSU.Geom.Size(totalWidth + 5, maxHeight + 5);
    }
    refreshLayout() {
        this.rootGroup.setAttribute("transform", "translate(" + this.x + "," + this.y + ")");
    }
    updateBounds(x, y, w, h) {
        return [x, y, NaN, NaN];
    }
}
exports.MarkerView = MarkerView;
class BeatView extends beatview_1.BeatView {
    createAtomView() {
        return (0, atomviews_1.createAtomView)(this.element, this.beat.atom, this.beat.role.defaultToNotes);
    }
    refreshLayout() {
        const newX = this.hasX ? this._x : 0;
        const newY = this.hasY ? this._y : 0;
        this.element.setAttribute("transform", "translate(" + newX + "," + newY + ")");
        if (this.hasWidth && this.atomView) {
            this.atomView.setBounds(0, 0, this.width, null, false);
            this.atomView.refreshLayout();
        }
        this.invalidateBounds();
        for (const e of this.embelishments)
            e.refreshLayout();
        this.invalidateBounds();
    }
    createEmbelishments() {
        let embelishments = [];
        const beat = this.beat;
        const rootElement = this.rootElement;
        if (beat.beatIndex == 0 && beat.barIndex == 0 && beat.instance == 0) {
            const emb = new embelishments_1.BeatStartLines(this, rootElement);
            embelishments = [emb];
        }
        else {
            const cycle = this.cycle;
            const bar = cycle.bars[beat.barIndex];
            if (beat.beatIndex == bar.beatCount - 1) {
                if (beat.instance == bar.instanceCount(beat.beatIndex) - 1) {
                    if (beat.barIndex == cycle.bars.length - 1) {
                        const emb = new embelishments_1.BeatEndLines(this, rootElement, 2);
                        embelishments = [emb];
                    }
                    else {
                        const emb = new embelishments_1.BeatEndLines(this, rootElement);
                        embelishments = [emb];
                    }
                }
            }
        }
        return embelishments;
    }
}
exports.BeatView = BeatView;
//# sourceMappingURL=beatviews.js.map