import * as TSU from "@panyam/tsutils";
import { ElementShape } from "../shapes";
import { createAtomView } from "./atomviews";
import { BeatView as BeatViewBase } from "../beatview";
import { BeatStartLines, BeatEndLines } from "./embelishments";
export class MarkerView extends 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];
    }
}
export class BeatView extends BeatViewBase {
    createAtomView() {
        return 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 + ")");
        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 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 BeatEndLines(this, rootElement, 2);
                        embelishments = [emb];
                    }
                    else {
                        const emb = new BeatEndLines(this, rootElement);
                        embelishments = [emb];
                    }
                }
            }
        }
        return embelishments;
    }
}
//# sourceMappingURL=beatviews.js.map