"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("../");
const PiAction_1 = require("../editor/PiAction");
class MatchUtil {
    static fullMatch(text, trigger) {
        if (PiAction_1.isRegExp(trigger)) {
            return trigger.test(text);
        }
        else if (__1.isString(trigger)) {
            return trigger === text;
        }
        else if (__1.isProKey(trigger)) {
            return false;
        }
        return false;
    }
    static partialMatch(text, trigger) {
        if (PiAction_1.isRegExp(trigger)) {
            const r = toPartialMatchRegex(trigger);
            const x = text.match(r);
            const b = exactlyOne(x, text);
            return b;
        }
        else if (__1.isString(trigger)) {
            return trigger.startsWith(text);
        }
        else if (__1.isProKey(trigger)) {
            return false;
        }
        return false;
    }
}
exports.MatchUtil = MatchUtil;
function exactlyOne(ra, text) {
    let result = false;
    if (ra === null) {
        result = false;
    }
    else {
        const arr = ra;
        if (arr.length === 1) {
            return arr[0] === text;
        }
        else if (arr.length > 1) {
            return false;
        }
    }
    return result;
}
function toPartialMatchRegex(re) {
    "use strict";
    const source = re.source;
    let i = 0;
    function process() {
        let result = "";
        let tmp;
        function appendRaw(nbChars) {
            result += source.substr(i, nbChars);
            i += nbChars;
        }
        function appendOptional(nbChars) {
            result += "(?:" + source.substr(i, nbChars) + "|$)";
            i += nbChars;
        }
        while (i < source.length) {
            switch (source[i]) {
                case "\\":
                    switch (source[i + 1]) {
                        case "c":
                            appendOptional(3);
                            break;
                        case "x":
                            appendOptional(4);
                            break;
                        case "u":
                            if (re.unicode) {
                                if (source[i + 2] === "{") {
                                    appendOptional(source.indexOf("}", i) - i + 1);
                                }
                                else {
                                    appendOptional(6);
                                }
                            }
                            else {
                                appendOptional(2);
                            }
                            break;
                        default:
                            appendOptional(2);
                            break;
                    }
                    break;
                case "[":
                    tmp = /\[(?:\\.|.)*?\]/g;
                    tmp.lastIndex = i;
                    tmp = tmp.exec(source);
                    appendOptional(tmp[0].length);
                    break;
                case "|":
                case "^":
                case "$":
                case "*":
                case "+":
                case "?":
                    appendRaw(1);
                    break;
                case "{":
                    tmp = /\{\d+,?\d*\}/g;
                    tmp.lastIndex = i;
                    tmp = tmp.exec(source);
                    if (tmp) {
                        appendRaw(tmp[0].length);
                    }
                    else {
                        appendOptional(1);
                    }
                    break;
                case "(":
                    if (source[i + 1] === "?") {
                        switch (source[i + 2]) {
                            case ":":
                                result += "(?:";
                                i += 3;
                                result += process() + "|$)";
                                break;
                            case "=":
                                result += "(?=";
                                i += 3;
                                result += process() + ")";
                                break;
                            case "!":
                                tmp = i;
                                i += 3;
                                process();
                                result += source.substr(tmp, i - tmp);
                                break;
                        }
                    }
                    else {
                        appendRaw(1);
                        result += process() + "|$)";
                    }
                    break;
                case ")":
                    ++i;
                    return result;
                default:
                    appendOptional(1);
                    break;
            }
        }
        return result;
    }
    return new RegExp(process(), re.flags);
}
//# sourceMappingURL=MatchUtil.js.map