"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 PiAction_1 = require("../editor/PiAction");
const PiLogging_1 = require("./PiLogging");
const LOGGER = new PiLogging_1.PiLogger("BehaviorUtils");
var PiCaretPosition;
(function (PiCaretPosition) {
    PiCaretPosition[PiCaretPosition["UNSPECIFIED"] = 0] = "UNSPECIFIED";
    PiCaretPosition[PiCaretPosition["LEFT_MOST"] = 1] = "LEFT_MOST";
    PiCaretPosition[PiCaretPosition["RIGHT_MOST"] = 2] = "RIGHT_MOST";
    PiCaretPosition[PiCaretPosition["INDEX"] = 3] = "INDEX";
})(PiCaretPosition = exports.PiCaretPosition || (exports.PiCaretPosition = {}));
class PiCaret {
    constructor(p, i) {
        this.position = p;
        this.index = i;
    }
    static IndexPosition(i) {
        return new PiCaret(PiCaretPosition.INDEX, i);
    }
}
exports.PiCaret = PiCaret;
PiCaret.RIGHT_MOST = new PiCaret(PiCaretPosition.RIGHT_MOST, 0);
PiCaret.LEFT_MOST = new PiCaret(PiCaretPosition.LEFT_MOST, 0);
PiCaret.UNSPECIFIED = new PiCaret(PiCaretPosition.UNSPECIFIED, 0);
var BehaviorExecutionResult;
(function (BehaviorExecutionResult) {
    BehaviorExecutionResult[BehaviorExecutionResult["EXECUTED"] = 0] = "EXECUTED";
    BehaviorExecutionResult[BehaviorExecutionResult["PARTIAL_MATCH"] = 1] = "PARTIAL_MATCH";
    BehaviorExecutionResult[BehaviorExecutionResult["NO_MATCH"] = 2] = "NO_MATCH";
})(BehaviorExecutionResult = exports.BehaviorExecutionResult || (exports.BehaviorExecutionResult = {}));
function executeBehavior(box, text, editor) {
    return __awaiter(this, void 0, void 0, function* () {
        let partialMatch = false;
        for (const a of editor.behaviors) {
            const trigger = a.trigger;
            if (a.activeInBoxRoles.includes(box.role)) {
                if (PiAction_1.isRegExp(trigger)) {
                    const matchArray = text.match(trigger);
                    LOGGER.log("MATCH " + text + " against " + trigger + "  results in " + (!!matchArray ? matchArray.length : "null"));
                    if (matchArray !== null && text === matchArray[0]) {
                        const execresult = yield a.execute(box, text, editor);
                        return BehaviorExecutionResult.EXECUTED;
                    }
                }
                else if (PiAction_1.isString(trigger)) {
                    if (trigger === text) {
                        LOGGER.log("MATCH FULL TEXT");
                        const execresult = yield a.execute(box, text, editor);
                        return BehaviorExecutionResult.EXECUTED;
                    }
                    else if (trigger.startsWith(text)) {
                        LOGGER.log("MATCH PARTIAL TEXT");
                        partialMatch = true;
                    }
                }
            }
        }
        LOGGER.info(this, "no alias match");
        if (partialMatch) {
            return BehaviorExecutionResult.PARTIAL_MATCH;
        }
        else {
            return BehaviorExecutionResult.NO_MATCH;
        }
    });
}
exports.executeBehavior = executeBehavior;
//# sourceMappingURL=BehaviorUtils.js.map