"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const mobx_1 = require("mobx");
const PiUtils_1 = require("../../util/PiUtils");
const ListBoxUtil_1 = require("../../util/ListBoxUtil");
const Box_1 = require("./Box");
const AliasBox_1 = require("./AliasBox");
const PiUtils_2 = require("../../util/PiUtils");
var Direction;
(function (Direction) {
    Direction["HORIZONTAL"] = "Horizontal";
    Direction["VERTICAL"] = "Vertical";
})(Direction || (Direction = {}));
class ListBox extends Box_1.Box {
    constructor(element, role, children, initializer) {
        super(element, role);
        this.direction = Direction.HORIZONTAL;
        this._children = [];
        PiUtils_2.PiUtils.initializeObject(this, initializer);
        if (children) {
            children.forEach(b => this.addChild(b));
        }
    }
    get children() {
        return this._children;
    }
    addChild(child) {
        if (!!child) {
            this._children.push(child);
            child.parent = this;
        }
        return this;
    }
    insertChild(child) {
        if (!!child) {
            this._children.splice(0, 0, child);
            child.parent = this;
        }
        return this;
    }
    addChildren(children) {
        if (children) {
            children.forEach(child => this.addChild(child));
        }
        return this;
    }
    nextSibling(box) {
        const index = this.children.indexOf(box);
        if (index !== -1) {
            if (index + 1 < this.children.length) {
                return this.children[index + 1];
            }
        }
        return null;
    }
    previousSibling(box) {
        const index = this.children.indexOf(box);
        if (index > 0) {
            return this.children[index - 1];
        }
        return null;
    }
    toString() {
        let result = "List: " + this.role + " " + this.direction.toString() + "<";
        for (const child of this.children) {
            result += "\n    " + child.toString();
        }
        result += ">";
        return result;
    }
}
__decorate([
    mobx_1.observable,
    __metadata("design:type", Array)
], ListBox.prototype, "_children", void 0);
exports.ListBox = ListBox;
class HorizontalListBox extends ListBox {
    constructor(element, role, children, initializer) {
        super(element, role, children, initializer);
        this.kind = "HorizontalListBox";
        this.direction = Direction.HORIZONTAL;
    }
}
exports.HorizontalListBox = HorizontalListBox;
class VerticalListBox extends ListBox {
    constructor(element, role, children, initializer) {
        super(element, role, children, initializer);
        this.kind = "VerticalListBox";
        this.direction = Direction.VERTICAL;
    }
}
exports.VerticalListBox = VerticalListBox;
class VerticalPiElementListBox extends VerticalListBox {
    constructor(element, role, list, listPropertyName, builder, editor, initializer) {
        super(element, role, [], initializer);
        this.kind = "VerticalPiElementListBox";
        this.list = list;
        this.editor = editor;
        this.listPropertyName = listPropertyName;
        this.insertElement = builder;
        this.init();
    }
    init() {
        let index = 0;
        for (const ent of this.list) {
            const line = new HorizontalListBox(ent, this.role + "-hor-" + index, [
                this.editor.projection.getBox(ent),
                new AliasBox_1.AliasBox(ent, "list-for-" + this.role, PiUtils_1.NBSP, {
                    selectable: true
                })
            ]);
            this.addChild(line);
            index++;
        }
        this.addChild(new AliasBox_1.AliasBox(this.element, "empty-list-for-" + this.role, PiUtils_1.NBSP, {
            placeholder: `<add ${this.role}>`
        }));
        this.editor.keyboardActions.splice(0, 0, ListBoxUtil_1.createKeyboardShortcutForList(this.role, this.insertElement, this.roleToSelectAfterCreation));
    }
}
exports.VerticalPiElementListBox = VerticalPiElementListBox;
function isHorizontalBox(b) {
    return b instanceof HorizontalListBox;
}
exports.isHorizontalBox = isHorizontalBox;
function isVerticalBox(b) {
    return b instanceof VerticalListBox;
}
exports.isVerticalBox = isVerticalBox;
//# sourceMappingURL=ListBox.js.map