"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("../");
const GridBox_1 = require("../editor/boxes/GridBox");
const Keys = require("../util/Keys");
const Keys_1 = require("../util/Keys");
const PiUtils_1 = require("./PiUtils");
const Styles_1 = require("../editor/components/styles/Styles");
class GridUtil {
    static createCollectionRowGrid(element, role, listPropertyName, list, columnNames, columnBoxes, builder, editor, initializer) {
        PiUtils_1.PiUtils.CHECK(element[listPropertyName] === list, "createCollectionRowGrid: listPropertyname should result in the list");
        const cells = [];
        columnNames.forEach((item, index) => {
            cells.push({
                row: 1,
                column: index + 1,
                box: new __1.LabelBox(element, "header" + index, () => item, {
                    style: Styles_1.STYLES.headerText,
                    selectable: false
                }),
                style: Styles_1.STYLES.header
            });
        });
        list.forEach((item, rowIndex) => {
            columnBoxes.forEach((projector, columnIndex) => {
                cells.push({
                    row: rowIndex + 2,
                    column: columnIndex + 1,
                    box: new __1.HorizontalListBox(item, "xx-" + columnIndex, [projector(item), new __1.AliasBox(item, "new-" + columnIndex, __1.NBSP)])
                });
            });
        });
        cells.push({
            row: list.length + 3,
            column: 1,
            columnSpan: columnBoxes.length,
            box: new __1.AliasBox(element, "alias-add-row", "<add new row>"),
            style: Styles_1.STYLES.header
        });
        editor.keyboardActions.splice(0, 0, this.createKeyboardShortcutForCollectionGrid(element, role, builder));
        editor.keyboardActions.splice(0, 0, this.createKeyboardShortcutForEmptyCollectionGrid(element, listPropertyName, builder, "textbox-name"));
        return new GridBox_1.GridBox(element, role, cells, initializer);
    }
    static createCollectionColumnGrid(element, role, list, columnNames, columnBoxes, builder, editor, initializer) {
        const cells = [];
        columnNames.forEach((item, index) => {
            cells.push({
                row: index + 1,
                column: 1,
                box: new __1.LabelBox(element, "header" + index, () => item, {
                    style: Styles_1.STYLES.header
                })
            });
        });
        list.forEach((item, rowIndex) => {
            columnBoxes.forEach((projector, columnIndex) => {
                cells.push({
                    column: rowIndex + 2,
                    row: columnIndex + 1,
                    box: new __1.HorizontalListBox(item, "xx-" + columnIndex, [projector(item), new __1.AliasBox(item, "new-" + columnIndex, __1.NBSP)])
                });
            });
        });
        editor.keyboardActions.splice(0, 0, this.createKeyboardShortcutForCollectionGrid(element, role, builder));
        return new GridBox_1.GridBox(element, role, cells, initializer);
    }
    static createKeyboardShortcutForCollectionGrid(container, collectionRole, elementCreator, roleToSelect) {
        const listKeyboardShortcut = {
            trigger: { meta: Keys_1.MetaKey.None, keyCode: Keys.ENTER },
            activeInBoxRoles: ["new-0", "new-1", "new-2", "new-3", "new-4", "new-5", "new-6", "new-7", "new-8", "new-9", "new-10"],
            action: (box, key, editor) => __awaiter(this, void 0, void 0, function* () {
                const element = box.element;
                const proc = element.piContainer();
                const parent = proc.container;
                PiUtils_1.PiUtils.CHECK(parent[proc.propertyName][proc.propertyIndex] === element);
                const newElement = elementCreator(box, editor);
                parent[proc.propertyName].splice(proc.propertyIndex + 1, 0, newElement);
                if (!!roleToSelect) {
                    yield editor.selectElement(newElement, roleToSelect);
                }
                else {
                    yield editor.selectElement(newElement);
                    yield editor.selectFirstEditableChildBox();
                }
                return null;
            })
        };
        return listKeyboardShortcut;
    }
    static createKeyboardShortcutForEmptyCollectionGrid(container, propertyRole, elementCreator, roleToSelect) {
        const listKeyboardShortcut = {
            trigger: { meta: Keys_1.MetaKey.None, keyCode: Keys.ENTER },
            activeInBoxRoles: ["alias-add-row"],
            action: (box, key, editor) => __awaiter(this, void 0, void 0, function* () {
                const element = box.element;
                const newElement = elementCreator(box, editor);
                element[propertyRole].push(newElement);
                if (!!roleToSelect) {
                    yield editor.selectElement(newElement, roleToSelect);
                }
                else {
                    yield editor.selectElement(newElement);
                    yield editor.selectFirstEditableChildBox();
                }
                return null;
            })
        };
        return listKeyboardShortcut;
    }
}
exports.GridUtil = GridUtil;
//# sourceMappingURL=GridUtil.js.map