"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Autocompletion = void 0;
const Named_js_1 = require("./Named.js");
class Autocompletion {
    constructor(pnOrForSymbol, symbolName) {
        if (symbolName !== undefined) {
            this.forSymbol = pnOrForSymbol;
            this.symbolName = symbolName;
        }
        else {
            const pn = pnOrForSymbol;
            this.forSymbol = pn.getSymbol();
            this.symbolName = pn.getName();
        }
    }
    isEmptyLiteral() {
        return (this instanceof Autocompletion.Literal) &&
            this.getCompletion(Autocompletion.Purpose.FOR_INSERTION).length == 0;
    }
    static literal(pn, literals, prefix, postfix) {
        if (prefix === undefined)
            prefix = "";
        if (postfix === undefined)
            postfix = "";
        return literals.map((l) => new Autocompletion.Literal(pn, prefix + l + postfix));
    }
    static parameterized(pn, parameterName) {
        return new Autocompletion.Parameterized(pn, parameterName).asArray();
    }
    static veto(pn) {
        return new Autocompletion.Veto(pn).asArray();
    }
    static doesAutocomplete(pn) {
        return new Autocompletion.DoesAutocomplete(pn).asArray();
    }
    getAlreadyEntered() {
        return this.alreadyEntered;
    }
    setAlreadyEntered(alreadyEntered) {
        this.alreadyEntered = alreadyEntered;
    }
    asArray() {
        return [this];
    }
}
exports.Autocompletion = Autocompletion;
(function (Autocompletion) {
    class Literal extends Autocompletion {
        constructor(pnOrForSymbol, sOrSymbolName, s) {
            if (s !== undefined) {
                super(pnOrForSymbol, sOrSymbolName);
                this.literal = s;
            }
            else {
                super(pnOrForSymbol);
                this.literal = sOrSymbolName;
            }
        }
        getCompletion(_purpose) {
            return this.literal;
        }
    }
    Autocompletion.Literal = Literal;
    class Parameterized extends Autocompletion {
        constructor(pnOrForSymbol, paramNameOrSymbolName, paramName) {
            if (paramName !== undefined) {
                super(pnOrForSymbol, paramNameOrSymbolName);
                this.paramName = paramName;
            }
            else {
                super(pnOrForSymbol);
                this.paramName = paramNameOrSymbolName;
            }
        }
        getCompletion(_purpose) {
            return "${" + this.paramName + "}";
        }
        getParamName() {
            return this.paramName;
        }
    }
    Autocompletion.Parameterized = Parameterized;
    class Veto extends Autocompletion {
        getCompletion(_purpose) {
            return Veto.VETO;
        }
    }
    Veto.VETO = "VETO";
    Autocompletion.Veto = Veto;
    class DoesAutocomplete extends Autocompletion {
        getCompletion(_purpose) {
            return "Something"; // the return value for DoesAutocomplete shouldn't matter
        }
    }
    Autocompletion.DoesAutocomplete = DoesAutocomplete;
    class EntireSequence extends Autocompletion {
        constructor(pnOrForSymbol, symbolName, sequence) {
            if (symbolName !== undefined && sequence != undefined) {
                super(pnOrForSymbol, symbolName);
                this.sequence = sequence;
            }
            else {
                super(pnOrForSymbol);
                this.sequence = pnOrForSymbol.getRule();
            }
            this.sequenceOfCompletions = [];
        }
        add(completions) {
            this.sequenceOfCompletions.push(completions);
        }
        getSequenceOfCompletions() {
            return this.sequenceOfCompletions;
        }
        getSequence() {
            return this.sequence;
        }
        addLiteral(symbol, name, completion) {
            this.add([new Literal(symbol, name, completion)]);
        }
        addParameterized(symbol, name, parameter) {
            this.add([new Parameterized(symbol, name, parameter)]);
        }
        getCompletion(purpose) {
            let autocompletionString = "";
            for (let i = 0; i < this.sequenceOfCompletions.length; i++) {
                const autocompletions = this.sequenceOfCompletions[i];
                const n = autocompletions.length;
                if (n > 1)
                    autocompletionString += "${" + this.sequence.getNameForChild(i) + "}";
                else if (n == 1) {
                    if (purpose === Purpose.FOR_MENU) {
                        let ins;
                        const ac = autocompletions[0];
                        if (ac instanceof Literal)
                            ins = ac.getCompletion(Purpose.FOR_INSERTION);
                        else
                            ins = "${" + this.sequence.getNameForChild(i) + "}";
                        if (ins === undefined || ins === Named_js_1.Named.UNNAMED)
                            ins = "${" + this.sequence.getChildren()[i].getSymbol() + "}";
                        autocompletionString += ins;
                    }
                    else if (purpose === Purpose.FOR_INSERTION)
                        autocompletionString += autocompletions[0].getCompletion(purpose);
                }
            }
            return autocompletionString;
        }
    }
    Autocompletion.EntireSequence = EntireSequence;
    let Purpose;
    (function (Purpose) {
        Purpose[Purpose["FOR_MENU"] = 0] = "FOR_MENU";
        Purpose[Purpose["FOR_INSERTION"] = 1] = "FOR_INSERTION";
    })(Purpose = Autocompletion.Purpose || (Autocompletion.Purpose = {}));
})(Autocompletion || (exports.Autocompletion = Autocompletion = {}));
//# sourceMappingURL=Autocompletion.js.map