import { Literal } from "./Terminal.js";
import { Named } from "./Named.js";
import { Autocompletion } from "./Autocompletion.js";
class DefaultParsedNode {
    constructor(matcher, symbol, production) {
        this.parent = undefined;
        this.children = [];
        this.matcher = matcher;
        this.symbol = symbol;
        this.production = production;
    }
    getSymbol() {
        return this.symbol;
    }
    getName() {
        return this.name !== undefined ? this.name : this.symbol.getSymbol();
    }
    setName(name) {
        this.name = name;
    }
    getProduction() {
        return this.production;
    }
    getMatcher() {
        return this.matcher;
    }
    doesAutocomplete() {
        let autocompletion = this.getAutocompletion(true);
        return autocompletion !== null && autocompletion !== undefined;
    }
    getAutocompletion(_justCheck) {
        if (this.symbol === null || this.symbol === undefined)
            return undefined;
        if (this.symbol instanceof Literal)
            return Autocompletion.literal(this, [this.symbol.getSymbol()]);
        let name = this.getName();
        if (name === Named.UNNAMED)
            name = this.symbol.getSymbol();
        if (this.symbol.isTerminal()) {
            return this.getParsedString().length > 0 ? Autocompletion.veto(this) : Autocompletion.parameterized(this, name);
        }
        return undefined;
    }
    numChildren() {
        return this.children.length;
    }
    getChildren() {
        return this.children;
    }
    getChild(n) {
        if (typeof (n) === 'number') {
            return this.children[n];
        }
        else if (typeof (n) === 'string') {
            return this.children.find((value) => value.getName() === n);
        }
        return undefined;
    }
    addChildren(...children) {
        this.children.push(...children);
        for (let child of children)
            child.parent = this;
    }
    getParent() {
        return this.parent;
    }
    removeAllChildren() {
        for (let child of this.children)
            child.parent = undefined;
        this.children.length = 0;
    }
    evaluate(...n) {
        if (n.length === 0) {
            if (this.symbol.isTerminal())
                return this.symbol.evaluate(this.getMatcher());
            return this.getParsedString();
        }
        if (typeof (n[0]) === 'number')
            return this.children[n].evaluate();
        if (typeof (n[0]) === 'string') {
            let pn = this;
            for (let name of n) {
                pn = pn.getChild(name);
                if (pn === null || pn === undefined)
                    return undefined;
            }
            return pn.evaluate();
        }
        return undefined;
    }
    getParsedString(...names) {
        let pn = this;
        for (let name of names) {
            pn = pn.getChild(name);
            if (pn === undefined)
                return "";
        }
        return pn.getMatcher().parsed;
    }
    toString() {
        return this.getMatcher().parsed;
    }
}
export { DefaultParsedNode };
//# sourceMappingURL=DefaultParsedNode.js.map