"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);
};
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 mobx_1 = require("mobx");
const PiLogging_1 = require("./PiLogging");
const PiModel_1 = require("../language/PiModel");
const PiEditor_1 = require("../editor/PiEditor");
exports.wait = (ms) => __awaiter(void 0, void 0, void 0, function* () { return new Promise(resolve => setTimeout(resolve, ms)); });
exports.NBSP = "".concat("\u00A0");
const LOGGER = new PiLogging_1.PiLogger("PiUtils");
class PiUtils {
    static initializeObject(target, source) {
        if (!(target && source)) {
            return;
        }
        Object.keys(source).forEach(key => {
            if (source.hasOwnProperty(key)) {
                target[key] = source[key];
            }
        });
    }
    static CHECK(b, msg) {
        if (!b) {
            throw new Error(msg ? "FAILED Check: " + msg : "check error");
        }
    }
    static setContainer(exp, piContainer, editor) {
        if (!!piContainer) {
            if (piContainer.propertyIndex === undefined) {
                piContainer.container[piContainer.propertyName] = exp;
            }
            else {
                piContainer.container[piContainer.propertyName][piContainer.propertyIndex] = exp;
            }
        }
        else {
            editor.rootElement = exp;
        }
    }
    static replaceExpression(oldExpression, newExpression, editor) {
        PiUtils.CHECK(PiModel_1.isPiExpression(oldExpression), "replaceExpression: old element should be a ProExpression, but it isn't");
        PiUtils.CHECK(PiModel_1.isPiExpression(newExpression), "replaceExpression: new element should be a ProExpression, but it isn't");
        PiUtils.setContainer(newExpression, oldExpression.piContainer(), editor);
    }
    static handleKeyboardShortcut(piKey, box, editor) {
        return __awaiter(this, void 0, void 0, function* () {
            for (const act of editor.keyboardActions) {
                if (act.trigger.meta === piKey.meta && act.trigger.keyCode === piKey.keyCode) {
                    if (act.activeInBoxRoles.includes(box.role)) {
                        LOGGER.log("handleKeyboardShortcut: executing keyboard action");
                        const selected = yield act.action(box, piKey, editor);
                        if (selected) {
                            editor.selectElement(selected, act.boxRoleToSelect);
                        }
                        return true;
                    }
                    else {
                        LOGGER.log("handleKeyboardShortcut: Keyboard action does not include role " + box.role);
                    }
                }
            }
            return false;
        });
    }
}
__decorate([
    mobx_1.action,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, PiEditor_1.PiEditor]),
    __metadata("design:returntype", void 0)
], PiUtils, "setContainer", null);
__decorate([
    mobx_1.action,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, PiEditor_1.PiEditor]),
    __metadata("design:returntype", void 0)
], PiUtils, "replaceExpression", null);
exports.PiUtils = PiUtils;
function initializeObject(target, source) {
    if (!(target && source)) {
        return;
    }
    Object.keys(source).forEach(key => {
        if (source.hasOwnProperty(key)) {
            target[key] = source[key];
        }
    });
}
exports.initializeObject = initializeObject;
//# sourceMappingURL=PiUtils.js.map