"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 Box_1 = require("./Box");
const PiUtils_1 = require("../../util/PiUtils");
class GridBox extends Box_1.Box {
    constructor(exp, role, cells, initializer) {
        super(exp, role);
        this.kind = "GridBox";
        this.$children = [];
        PiUtils_1.PiUtils.initializeObject(this, initializer);
        this.cells = cells;
        this.cells.forEach(c => {
            if (!c.columnSpan) {
                c.columnSpan = 1;
            }
            if (!c.rowSpan) {
                c.rowSpan = 1;
            }
        });
        this.sortCellsAndAddChildren();
    }
    get children() {
        return this.$children;
    }
    sortCellsAndAddChildren() {
        this.cells = this.cells.sort(compare);
        this.cells.forEach(cell => {
            this.addChild(cell.box);
        });
    }
    numberOfColumns() {
        let max = 0;
        this.cells.forEach(c => (max = Math.max(max, c.column + c.columnSpan)));
        return max;
    }
    numberOfRows() {
        let max = 0;
        this.cells.forEach(cell => (max = Math.max(max, cell.row + cell.rowSpan)));
        return max;
    }
    addChild(child) {
        if (!!child) {
            this.$children.push(child);
            child.parent = this;
        }
    }
}
__decorate([
    mobx_1.observable,
    __metadata("design:type", Array)
], GridBox.prototype, "$children", void 0);
exports.GridBox = GridBox;
function compare(a, b) {
    if (a.row < b.row) {
        return -1;
    }
    else if (a.row > b.row) {
        return 1;
    }
    else {
        return a.column - b.column;
    }
}
function isGridBox(box) {
    return box instanceof GridBox;
}
exports.isGridBox = isGridBox;
//# sourceMappingURL=GridBox.js.map