"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 PiUtils_1 = require("./PiUtils");
const Keys_1 = require("./Keys");
const AliasBox_1 = require("../editor/boxes/AliasBox");
const ListBox_1 = require("../editor/boxes/ListBox");
const PiLogging_1 = require("./PiLogging");
const Keys = require("./Keys");
const LOGGER = new PiLogging_1.PiLogger("ListBoxUtil");
function boxAbove(box) {
    PiUtils_1.wait(0);
    const x = box.actualX;
    const y = box.actualY;
    let result = box.nextLeafLeft;
    let tmpResult = result;
    LOGGER.log("boxAbove: " + x + ", " + y);
    while (result !== null) {
        LOGGER.log("previous : " + result.role + "  " + result.actualX + ", " + result.actualY);
        if (isOnPreviousLine(tmpResult, result) && isOnPreviousLine(box, tmpResult)) {
            return tmpResult;
        }
        if (isOnPreviousLine(box, result)) {
            if (result.actualX <= x) {
                return result;
            }
        }
        const next = result.nextLeafLeft;
        tmpResult = result;
        result = next;
    }
    return result;
}
exports.boxAbove = boxAbove;
function boxBelow(box) {
    const x = box.actualX;
    const y = box.actualY;
    let result = box.nextLeafRight;
    let tmpResult = result;
    LOGGER.log("boxBelow " + box.role + ": " + x + ", " + y);
    while (result !== null) {
        LOGGER.log("next : " + result.role + "  " + result.actualX + ", " + result.actualY);
        if (isOnNextLine(tmpResult, result) && isOnNextLine(box, tmpResult)) {
            LOGGER.log("Found box below 1 [" + (!!tmpResult ? tmpResult.role : "null") + "]");
            return tmpResult;
        }
        if (isOnNextLine(box, result)) {
            if (result.actualX + result.actualWidth >= x) {
                LOGGER.log("Found box below 2 [" + (!!result ? result.role : "null") + "]");
                return result;
            }
        }
        const next = result.nextLeafRight;
        tmpResult = result;
        result = next;
    }
    LOGGER.log("Found box below 3 [" + (!!result ? result.role : "null") + "]");
    return result;
}
exports.boxBelow = boxBelow;
function isOnPreviousLine(ref, other) {
    const margin = 5;
    return other.actualY + 5 < ref.actualY;
}
function isOnNextLine(ref, other) {
    return isOnPreviousLine(other, ref);
}
function firstVerticalBoxParent(box) {
    const resultL = [];
    resultL.push(box);
    let result = box;
    while (result.parent) {
        result = result.parent;
        resultL.push(result);
        if (result instanceof ListBox_1.VerticalListBox) {
            return resultL;
        }
    }
    return resultL;
}
exports.firstVerticalBoxParent = firstVerticalBoxParent;
function createVerticalListBox(element, role, list, placeholderRole, elementCreator, editor) {
    LOGGER.info(this, "createVerticalListBox");
    const result = new ListBox_1.VerticalListBox(element, role, []);
    for (let index = 0; index < list.length; index++) {
        const ent = list[index];
        const line = new ListBox_1.HorizontalListBox(element, role + "-hor-" + index, [
            editor.projection.getBox(ent),
            new AliasBox_1.AliasBox(ent, "list-for-" + role, "    ", { roleNumber: index })
        ]);
        result.addChild(line);
    }
    return result;
}
exports.createVerticalListBox = createVerticalListBox;
function createKeyboardShortcutForList(collectionRole, elementCreator, roleToSelect) {
    const listKeyboardShortcut = {
        trigger: { meta: Keys_1.MetaKey.None, keyCode: Keys.ENTER },
        activeInBoxRoles: ["list-for-" + collectionRole],
        action: (box, key, editor) => __awaiter(this, void 0, void 0, function* () {
            LOGGER.log("createKeyboardShortcutForList: Action: list-for-" + collectionRole);
            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) {
                LOGGER.log("List select element for role " + roleToSelect);
                yield editor.selectElement(newElement, roleToSelect);
            }
            else {
                LOGGER.log("List select first leaf " + roleToSelect);
                yield editor.selectElement(newElement);
                LOGGER.log("List select first editable leaf NOW " + roleToSelect);
                yield editor.selectFirstEditableChildBox();
            }
            return null;
        })
    };
    return listKeyboardShortcut;
}
exports.createKeyboardShortcutForList = createKeyboardShortcutForList;
//# sourceMappingURL=ListBoxUtil.js.map