"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EMPTY_TREE = exports.OBJECT_TREE = exports.REGEX_TREE = exports.ANY_TREE = exports.NUMBER_TREE = exports.BOOLEAN_TREE = exports.STRING_TREE = exports.NULL_TREE = exports.checkClassInheritance = exports.PARENTHESIS_CLOSE = exports.PARENTHESIS_OPEN = exports.ARRAY_CLOSE = exports.ARRAY_OPEN = exports.OBJECT_CLOSE = exports.OBJECT_OPEN = exports.SINGLE_QUOTE = exports.DOUBLE_QUOTE = exports.COMMA = exports.TreeNodeType = void 0;
exports.traverseTree = traverseTree;
exports.printTree = printTree;
exports.isCompositeType = isCompositeType;
exports.isGeneric = isGeneric;
exports.hasGeneric = hasGeneric;
exports._isGeneric = _isGeneric;
exports._hasGeneric = _hasGeneric;
exports._isValueOfType = _isValueOfType;
exports.isValueOfType = isValueOfType;
exports.isValidType = isValidType;
exports.getTreeNodeType = getTreeNodeType;
exports.isLiteralType = isLiteralType;
exports.parseClassValue = parseClassValue;
exports.getLiteralType = getLiteralType;
exports._isTypeMatch = _isTypeMatch;
exports.isTypeMatch = isTypeMatch;
exports._isValidTree = _isValidTree;
exports.isValidTree = isValidTree;
exports._isValidValue = _isValidValue;
exports._isValidType = _isValidType;
exports.isValidValue = isValidValue;
exports.isValidObjValue = isValidObjValue;
exports._isValidObjKeyType = _isValidObjKeyType;
exports.isValidObjKey = isValidObjKey;
exports._isKey = _isKey;
exports.isKey = isKey;
exports.isValidBooleanStr = isValidBooleanStr;
exports.isValidNumberStr = isValidNumberStr;
exports.isValidStringStr = isValidStringStr;
exports.isValidIdentifierStr = isValidIdentifierStr;
exports.isValidKeyStr = isValidKeyStr;
exports.getTree = getTree;
exports._getTypeTree = _getTypeTree;
exports.getValueTree = getValueTree;
exports._extractGenerics = _extractGenerics;
exports._getValueType = _getValueType;
exports.getValueType = getValueType;
exports._stringify = _stringify;
exports._filterEmptyNodes = _filterEmptyNodes;
exports.filterEmptyNodes = filterEmptyNodes;
exports.findGenerics = findGenerics;
exports._findGenerics = _findGenerics;
exports.extractGenerics = extractGenerics;
exports._applyGenerics = _applyGenerics;
exports.applyGenerics = applyGenerics;
exports._getNodeAtPath = _getNodeAtPath;
exports.getNodeAtPath = getNodeAtPath;
exports._getNextNode = _getNextNode;
exports.getNextNode = getNextNode;
exports._getNextNodePath = _getNextNodePath;
exports.getNextNodePath = getNextNodePath;
exports._getNextLeafPath = _getNextLeafPath;
exports.getNextLeafPath = getNextLeafPath;
exports._getNextSiblingPath = _getNextSiblingPath;
exports.getNextSiblingPath = getNextSiblingPath;
exports.getParentPath = getParentPath;
exports.getParent = getParent;
exports._getLastLeaf = _getLastLeaf;
exports._getLastLeafPath = _getLastLeafPath;
exports.getLastLeafPath = getLastLeafPath;
exports.getLastLeaf = getLastLeaf;
exports._getParent = _getParent;
exports._insertNodeAt = _insertNodeAt;
exports.insertNodeAt = insertNodeAt;
exports._updateNodeAt = _updateNodeAt;
exports.updateNodeAt = updateNodeAt;
exports._removeNodeAt = _removeNodeAt;
exports.removeNodeAt = removeNodeAt;
exports._matchAllExcTypes = _matchAllExcTypes;
exports.matchAllExcTypes = matchAllExcTypes;
exports.findAllUnitNodes = findAllUnitNodes;
exports.findAndReplaceUnitNodes_ = findAndReplaceUnitNodes_;
exports.findAndReplaceUnitNodes = findAndReplaceUnitNodes;
exports.findAllNodes = findAllNodes;
const MethodNotImplementedError_1 = require("../exception/MethodNotImplementedError");
const inheritance_1 = require("../inheritance");
const f_1 = require("../system/f/object/Keys/f");
const array_1 = require("../util/array");
const clone_1 = require("../util/clone");
const string_1 = require("../util/string");
const weakMerge_1 = require("../weakMerge");
const idFromUnitValue_1 = require("./idFromUnitValue");
const BOOLEAN_LITERAL_1 = require("./regex/BOOLEAN_LITERAL");
const IDENTIFIER_1 = require("./regex/IDENTIFIER");
const NUMBER_LITERAL_1 = require("./regex/NUMBER_LITERAL");
const STRING_LITERAL_1 = require("./regex/STRING_LITERAL");
var TreeNodeType;
(function (TreeNodeType) {
    TreeNodeType["Invalid"] = "invalid";
    TreeNodeType["Null"] = "null";
    TreeNodeType["Generic"] = "generic";
    TreeNodeType["Identifier"] = "identifier";
    TreeNodeType["Expression"] = "expression";
    TreeNodeType["ArrayExpression"] = "array expression";
    TreeNodeType["ObjectExpression"] = "object expression";
    TreeNodeType["Or"] = "or";
    TreeNodeType["And"] = "and";
    TreeNodeType["Object"] = "object";
    TreeNodeType["String"] = "string";
    TreeNodeType["Number"] = "number";
    TreeNodeType["Boolean"] = "boolean";
    TreeNodeType["Class"] = "class";
    TreeNodeType["StringLiteral"] = "string literal";
    TreeNodeType["BooleanLiteral"] = "boolean literal";
    TreeNodeType["NumberLiteral"] = "number literal";
    TreeNodeType["ObjectLiteral"] = "object literal";
    TreeNodeType["ArrayLiteral"] = "array literal";
    TreeNodeType["ClassLiteral"] = "class literal";
    TreeNodeType["KeyValue"] = "key value";
    TreeNodeType["Regex"] = "regex";
    TreeNodeType["RegexLiteral"] = "regex literal";
    TreeNodeType["Date"] = "date";
    TreeNodeType["DateLiteral"] = "date literal";
    TreeNodeType["Time"] = "time";
    TreeNodeType["Unit"] = "unit";
    TreeNodeType["Any"] = "any";
    TreeNodeType["PropExpression"] = "prop expression";
    TreeNodeType["Placeholder"] = "placeholder";
    TreeNodeType["ArithmeticExpression"] = "arithmetic expression";
    TreeNodeType["Url"] = "url";
})(TreeNodeType || (exports.TreeNodeType = TreeNodeType = {}));
exports.COMMA = ',';
exports.DOUBLE_QUOTE = '"';
exports.SINGLE_QUOTE = "'";
exports.OBJECT_OPEN = '{';
exports.OBJECT_CLOSE = '}';
exports.ARRAY_OPEN = '[';
exports.ARRAY_CLOSE = ']';
exports.PARENTHESIS_OPEN = '(';
exports.PARENTHESIS_CLOSE = ')';
function trimSides(str) {
    return str.substring(1, str.length - 1);
}
function traverseTree(root, callback, path = []) {
    callback(root, path);
    root.children.forEach((child, index) => traverseTree(child, callback, [...path, index]));
}
function _printTree(tree) {
    // console.log(JSON.stringify(tree, null, 2))
    traverseTree(tree, (node, path) => 
    // eslint-disable-next-line no-console
    console.log(path, node.value, node.type));
}
function printTree(data) {
    const tree = getTree(data);
    _printTree(tree);
}
function isCompositeType(type) {
    return ([
        TreeNodeType.Or,
        TreeNodeType.And,
        TreeNodeType.ArrayExpression,
        TreeNodeType.Expression,
        TreeNodeType.ObjectLiteral,
        TreeNodeType.ArrayLiteral,
        TreeNodeType.KeyValue,
        TreeNodeType.Class,
    ].indexOf(type) > -1);
}
function isGeneric(type) {
    return hasGeneric(type);
}
function hasGeneric(type) {
    const tree = getTree(type);
    return _hasGeneric(tree);
}
function _isGeneric(tree) {
    return _hasGeneric(tree);
}
function _hasGeneric(tree) {
    return (tree.type === TreeNodeType.Generic ||
        (isCompositeType(tree.type) && tree.children.some(_hasGeneric)));
}
function getObjLiteralKeyValueMap(tree) {
    const keyValueMap = {};
    for (const keyValue of tree.children) {
        if (keyValue.type === TreeNodeType.KeyValue) {
            const keyTree = keyValue.children[0];
            let key;
            const { value, type } = keyTree;
            if (type === TreeNodeType.Identifier) {
                key = value;
            }
            else {
                key = value.substr(1, value.length - 2);
            }
            keyValueMap[key] = keyValue.children[1];
        }
        else {
            keyValueMap[keyValue.value] = keyValue.value;
        }
    }
    return keyValueMap;
}
function _isValueOfType(value, type) {
    switch (type.type) {
        case TreeNodeType.Generic:
            return _isValidValue(value);
        case TreeNodeType.Identifier:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
            return value.type === type.type && value.value === type.value;
        case TreeNodeType.ObjectLiteral: {
            const typeKeyValueMap = getObjLiteralKeyValueMap(type);
            const valueKeyValueMap = getObjLiteralKeyValueMap(value);
            for (const key in typeKeyValueMap) {
                if (!valueKeyValueMap[key] ||
                    !_isValueOfType(valueKeyValueMap[key], typeKeyValueMap[key])) {
                    return false;
                }
            }
            return value.type === TreeNodeType.ObjectLiteral;
        }
        case TreeNodeType.ArrayLiteral:
            return (value.type === TreeNodeType.ArrayLiteral &&
                !value.children.some((child, index) => !_isValueOfType(child, type.children[index])));
        case TreeNodeType.Object:
            return value.type === TreeNodeType.ObjectLiteral;
        case TreeNodeType.String:
            return value.type === TreeNodeType.StringLiteral;
        case TreeNodeType.Number:
            return value.type === TreeNodeType.NumberLiteral;
        case TreeNodeType.Boolean:
            return value.type === TreeNodeType.BooleanLiteral;
        case TreeNodeType.Or:
            return type.children.some((child) => _isValueOfType(value, child));
        case TreeNodeType.And:
            return !type.children.some((child) => !_isValueOfType(value, child));
        case TreeNodeType.ArrayExpression:
            return (value.type === TreeNodeType.ArrayLiteral &&
                !value.children.some((child) => !_isValueOfType(child, type.children[0])));
        case TreeNodeType.ObjectExpression:
            return (value.type === TreeNodeType.ObjectLiteral &&
                !value.children.some((child) => !_isValueOfType(child, type.children[0])));
        case TreeNodeType.Expression:
            return _isValueOfType(value, type.children[0]);
        case TreeNodeType.KeyValue:
            return (_isValueOfType(value.children[0], type.children[0]) &&
                _isValueOfType(value.children[1], type.children[1]));
        default:
            throw new Error('not a valid TreeNodeType');
    }
}
function isValueOfType(value, type) {
    const valueTree = getTree(value);
    const typeTree = getTree(type);
    if (!_isValidValue(valueTree)) {
        return false;
    }
    if (!_isValidType(typeTree)) {
        return false;
    }
    return _isValueOfType(valueTree, typeTree);
}
function isValidType(type) {
    const tree = getTree(type);
    return _isValidType(tree);
}
function getTreeNodeType(value) {
    const tree = getTree(value);
    return tree.type;
}
function isLiteralType(type) {
    return !!LITERAL_TO_TYPE[type];
}
const LITERAL_TO_TYPE = {
    [TreeNodeType.NumberLiteral]: TreeNodeType.Number,
    [TreeNodeType.StringLiteral]: TreeNodeType.String,
    [TreeNodeType.BooleanLiteral]: TreeNodeType.Boolean,
    [TreeNodeType.RegexLiteral]: TreeNodeType.Regex,
    [TreeNodeType.DateLiteral]: TreeNodeType.Date,
};
const TYPE_TO_LITERAL = {
    [TreeNodeType.Number]: TreeNodeType.NumberLiteral,
    [TreeNodeType.String]: TreeNodeType.StringLiteral,
    [TreeNodeType.Boolean]: TreeNodeType.BooleanLiteral,
    [TreeNodeType.Object]: TreeNodeType.ObjectLiteral,
    [TreeNodeType.ArrayExpression]: TreeNodeType.ArrayLiteral,
    [TreeNodeType.Regex]: TreeNodeType.RegexLiteral,
    [TreeNodeType.Date]: TreeNodeType.DateLiteral,
};
function parseClassValue(tree) {
    const child = tree.children[0] ?? getTree('<any>');
    const diamond = child.value;
    const placeholder = child.type === TreeNodeType.Generic
        ? child
        : getTree(child.value.slice(1, -1));
    const base = tree.value.replace(diamond, '').slice(1, -1);
    return { base, placeholder };
}
function getLiteralType(type) {
    return TYPE_TO_LITERAL[type];
}
const checkClassInheritance = (sourceClass, targetClass) => {
    if (sourceClass === targetClass) {
        return true;
    }
    const parentClasses = inheritance_1.INHERITANCE[sourceClass];
    if (parentClasses) {
        return parentClasses.some((parentClass) => (0, exports.checkClassInheritance)(parentClass, targetClass));
    }
    return false;
};
exports.checkClassInheritance = checkClassInheritance;
function _isTypeMatch(specs, source, target) {
    if (source.type === TreeNodeType.Invalid) {
        return false;
    }
    if (source.type === TreeNodeType.Identifier) {
        return false;
    }
    if (source.type === TreeNodeType.Any) {
        return true;
    }
    if (source.type === TreeNodeType.Generic) {
        return true;
    }
    if (source.type === TreeNodeType.Or) {
        return !source.children.some((sourceChild) => {
            return !_isTypeMatch(specs, sourceChild, target);
        });
    }
    if (source.type === TreeNodeType.And) {
        if (target.type === TreeNodeType.And) {
            return target.children.every((targetChild) => {
                return source.children.some((sourceChild) => {
                    const typeMatch = _isTypeMatch(specs, sourceChild, targetChild);
                    return typeMatch;
                });
            });
        }
        else {
            return source.children.some((sourceChild) => {
                return _isTypeMatch(specs, sourceChild, target);
            });
        }
    }
    if (source.type === TreeNodeType.Expression) {
        return _isTypeMatch(specs, source.children[0], target);
    }
    if (source.type === TreeNodeType.Class) {
        if (source.value.startsWith('`V')) {
            if (target.value.startsWith('`J') || target.value.startsWith('`A')) {
                return true;
            }
        }
    }
    switch (target.type) {
        case TreeNodeType.Invalid:
            return false;
        case TreeNodeType.Identifier:
            return false;
        case TreeNodeType.Any:
            return true;
        case TreeNodeType.Generic:
            return true;
        case TreeNodeType.String:
            return (source.type === TreeNodeType.String ||
                source.type === TreeNodeType.StringLiteral);
        case TreeNodeType.Number:
            return (source.type === TreeNodeType.Number ||
                source.type === TreeNodeType.NumberLiteral);
        case TreeNodeType.Boolean:
            return (source.type === TreeNodeType.Boolean ||
                source.type === TreeNodeType.BooleanLiteral);
        case TreeNodeType.Object:
            return (source.type === TreeNodeType.Object ||
                (source.type === TreeNodeType.ObjectLiteral && _isValidTree(source)));
        case TreeNodeType.Regex:
            return (source.type === TreeNodeType.Regex ||
                source.type === TreeNodeType.RegexLiteral);
        case TreeNodeType.Class:
            if (source.type === TreeNodeType.Class) {
                const { base: sourceClass, placeholder: sourcePlaceholder } = parseClassValue(source);
                const { base: targetClass, placeholder: targetPlaceholder } = parseClassValue(target);
                const match = (0, exports.checkClassInheritance)(sourceClass, targetClass);
                if (match) {
                    if (_isTypeMatch(specs, sourcePlaceholder, targetPlaceholder)) {
                        return true;
                    }
                }
                return false;
            }
            else if (source.type === TreeNodeType.Unit) {
                const bundle = (0, idFromUnitValue_1.evaluateBundleStr)(source.value, specs, {});
                const specId = bundle.unit.id;
                const _specs = (0, weakMerge_1.weakMerge)(specs, bundle.specs ?? {});
                const spec = _specs[specId];
                const { type = '`U`&`C`&`G`' } = spec;
                const typeTree = getTree(type);
                return _isTypeMatch(specs, typeTree, target);
            }
            else {
                return false;
            }
        case TreeNodeType.Null:
            return source.type === TreeNodeType.Null;
        case TreeNodeType.Unit:
            return source.type === TreeNodeType.Unit;
        case TreeNodeType.ArithmeticExpression:
            return source.type === TreeNodeType.ArithmeticExpression;
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.StringLiteral:
            return (source.type === target.type &&
                source.value.substr(1, source.value.length - 2) ===
                    target.value.substr(1, target.value.length - 2));
        case TreeNodeType.ObjectLiteral:
            if (source.type === TreeNodeType.Object) {
                return true;
            }
            else if (source.type === TreeNodeType.ObjectLiteral) {
                const sourceDict = getObjLiteralKeyValueMap(source);
                const targetDict = getObjLiteralKeyValueMap(target);
                for (const targetKey in targetDict) {
                    const sourceValue = sourceDict[targetKey];
                    const targetValue = targetDict[targetKey];
                    if (targetKey.endsWith('?')) {
                        if (sourceValue !== undefined &&
                            !_isTypeMatch(specs, sourceValue, targetValue)) {
                            return false;
                        }
                    }
                    else if (sourceValue === undefined) {
                        return false;
                    }
                    else if (sourceValue.value === '') {
                        if (!_isTypeMatch(specs, getTree(`"${targetKey}"`), targetValue)) {
                            return false;
                        }
                    }
                    else if (!_isTypeMatch(specs, sourceValue, targetValue)) {
                        return false;
                    }
                }
                return true;
            }
            else {
                return false;
            }
        case TreeNodeType.ArrayLiteral:
            if (source.type !== TreeNodeType.ArrayLiteral) {
                return false;
            }
            for (let i = 0; i < target.children.length; i++) {
                const targetChild = target.children[i];
                const sourceChild = source.children[i];
                if (!sourceChild || !_isTypeMatch(specs, sourceChild, targetChild)) {
                    return false;
                }
            }
            return true;
        case TreeNodeType.Or:
            return target.children.some((targetChild) => _isTypeMatch(specs, source, targetChild));
        case TreeNodeType.And:
            return !target.children.some((targetChild) => !_isTypeMatch(specs, source, targetChild));
        case TreeNodeType.Expression:
            return _isTypeMatch(specs, source, target.children[0]);
        case TreeNodeType.ArrayExpression:
            return ((source.type === TreeNodeType.ArrayExpression &&
                _isTypeMatch(specs, source.children[0], target.children[0])) ||
                (source.type === TreeNodeType.ArrayLiteral &&
                    (source.children.length === 0 ||
                        !source.children.some((c) => !_isTypeMatch(specs, c, target.children[0])))));
        case TreeNodeType.ObjectExpression:
            return (source.type === TreeNodeType.Object ||
                (source.type === TreeNodeType.ObjectExpression &&
                    _isTypeMatch(specs, source.children[0], target.children[0])) ||
                (source.type === TreeNodeType.ObjectLiteral &&
                    (source.children.length === 0 ||
                        !source.children.some((line) => {
                            if (line.type === TreeNodeType.KeyValue) {
                                return !_isTypeMatch(specs, line.children[1], target.children[0]);
                            }
                            else if (line.type === TreeNodeType.String ||
                                line.type === TreeNodeType.Identifier) {
                                return false;
                            }
                        }))));
    }
    return false;
}
function isTypeMatch(specs, source, target) {
    const sourceTree = getTree(source);
    const targetTree = getTree(target);
    return _isTypeMatch(specs, sourceTree, targetTree);
}
function _isValidTree(value) {
    return _isValidType(value) || _isValidValue(value);
}
function isValidTree(value) {
    const tree = getTree(value);
    return _isValidType(tree) || _isValidValue(tree);
}
function _isValidValue(tree, _getTree = _getValueTree) {
    switch (tree.type) {
        case TreeNodeType.Invalid:
        case TreeNodeType.Generic:
        case TreeNodeType.Identifier:
        case TreeNodeType.String:
        case TreeNodeType.Number:
        case TreeNodeType.Boolean:
        case TreeNodeType.Object:
        case TreeNodeType.Or:
        case TreeNodeType.And:
        case TreeNodeType.ArrayExpression:
        case TreeNodeType.ObjectExpression:
        case TreeNodeType.Expression:
        case TreeNodeType.KeyValue:
            return false;
        case TreeNodeType.Null:
        case TreeNodeType.Unit:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.RegexLiteral:
        case TreeNodeType.DateLiteral:
        case TreeNodeType.ClassLiteral:
        case TreeNodeType.ArithmeticExpression:
            return true;
        case TreeNodeType.ObjectLiteral:
            return !tree.children.some((element, index) => {
                if (element.type === TreeNodeType.KeyValue) {
                    const { children: [key_tree, value_tree], } = element;
                    if (!key_tree) {
                        return true;
                    }
                    if (key_tree.value === '') {
                        return true;
                    }
                    if (!_isValidObjKeyType(key_tree)) {
                        return true;
                    }
                    if (value_tree.value !== '' && !_isValidValue(value_tree, _getTree)) {
                        return true;
                    }
                    return false;
                }
                else if (_isValidObjKeyType(element)) {
                    return false;
                }
                else if (element.value === '' && index === tree.children.length - 1) {
                    return false;
                }
                return true;
            });
        case TreeNodeType.ArrayLiteral:
            return !tree.children.some((element, index) => {
                if (!_isValidValue(element, _getTree) &&
                    (element.value !== '' || index !== tree.children.length - 1)) {
                    return true;
                }
                return false;
            });
        default:
            return false;
    }
}
function _isValidType(tree) {
    switch (tree.type) {
        case TreeNodeType.Invalid:
        case TreeNodeType.Identifier:
        case TreeNodeType.KeyValue:
            return false;
        case TreeNodeType.Generic:
        case TreeNodeType.String:
        case TreeNodeType.Number:
        case TreeNodeType.Regex:
        case TreeNodeType.Date:
        case TreeNodeType.Boolean:
        case TreeNodeType.Object:
        case TreeNodeType.Null:
        case TreeNodeType.Class:
        case TreeNodeType.Any:
        case TreeNodeType.Unit:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.RegexLiteral:
        case TreeNodeType.DateLiteral:
        case TreeNodeType.ClassLiteral:
        case TreeNodeType.ArithmeticExpression:
            return true;
        case TreeNodeType.ArrayExpression:
        case TreeNodeType.ObjectExpression:
        case TreeNodeType.PropExpression:
        case TreeNodeType.Expression:
        case TreeNodeType.Or:
        case TreeNodeType.And:
            return !tree.children.some((child) => !_isValidType(child));
        case TreeNodeType.ObjectLiteral:
            return !tree.children.some((element, index) => {
                if (element.type === TreeNodeType.KeyValue) {
                    const { children: [key, value], } = element;
                    if (key.value === '') {
                        return true;
                    }
                    return ((!key ||
                        !_isValidObjKeyType(key) ||
                        !value ||
                        !_isValidType(value)) &&
                        (element.value !== '' || index !== tree.children.length - 1));
                }
                else if (_isValidObjKeyType(element) ||
                    (element.value === '' && index === tree.children.length - 1)) {
                    return false;
                }
                return true;
            });
        case TreeNodeType.ArrayLiteral:
            return !tree.children.some((element, index) => !_isValidType(element) &&
                (element.value !== '' || index !== tree.children.length - 1));
        default:
            return false;
    }
}
function isValidValue(value) {
    const tree = getTree(value);
    return _isValidValue(tree);
}
function _isValidObjValueType(type) {
    return type !== TreeNodeType.Identifier;
}
function isValidObjValue(type) {
    const tree = getTree(type);
    return _isValidObjValueType(tree.type);
}
function _isValidObjKeyType(tree) {
    return (tree.type === TreeNodeType.Generic ||
        tree.type === TreeNodeType.Identifier ||
        tree.type === TreeNodeType.StringLiteral ||
        tree.type === TreeNodeType.BooleanLiteral ||
        tree.type === TreeNodeType.NumberLiteral);
}
function isValidObjKey(key) {
    const tree = getTree(key);
    return _isValidObjKeyType(tree);
}
function _isKey(root, path) {
    const parent = _getParent(root, path);
    return !!(parent &&
        parent.type === TreeNodeType.KeyValue &&
        path[path.length - 1] === 0);
}
function isKey(root, path) {
    const tree = getTree(root);
    return _isKey(tree, path);
}
function isValidBooleanStr(str) {
    return BOOLEAN_LITERAL_1.BOOLEAN_LITERAL_REGEX.test(str);
}
function isValidNumberStr(str) {
    return NUMBER_LITERAL_1.NUMBER_LITERAL_REGEX.test(str);
}
function isValidStringStr(str) {
    return STRING_LITERAL_1.STRING_LITERAL_REGEX.test(str);
}
function isValidIdentifierStr(str) {
    return IDENTIFIER_1.IDENTIFIER_REGEX.test(str);
}
function isValidKeyStr(str) {
    return (isValidBooleanStr(str) ||
        isValidNumberStr(str) ||
        isValidStringStr(str) ||
        isValidIdentifierStr(str));
}
function getTree(value, keyValue = false, ignoreKeyword = false) {
    return (_getValueTree(value, keyValue, ignoreKeyword, getTree) ||
        _getTypeTree(value, keyValue, ignoreKeyword) || {
        value,
        type: TreeNodeType.Invalid,
        children: [],
    });
}
function _getTypeTree(value, keyValue = false, ignoreKeyword = false) {
    value = value.trim();
    if (!ignoreKeyword) {
        const classLiteralTest = /^\((.*)\)=>\((.*)\)$/.exec(value);
        if (classLiteralTest) {
            return {
                value,
                type: TreeNodeType.ClassLiteral,
                children: [],
            };
        }
    }
    const expressionTest = /^\((.+)\)$/.exec(value);
    if (expressionTest) {
        const body = expressionTest[1];
        let open = 0;
        let valid = true;
        for (let i = 0; i < body.length; i++) {
            const char = body[i];
            if (char === '(') {
                open++;
            }
            else if (char === ')') {
                open--;
            }
            if (open < 0) {
                valid = false;
                break;
            }
        }
        if (valid) {
            const children = [getTree(body)];
            return {
                value,
                type: TreeNodeType.Expression,
                children,
            };
        }
    }
    const genericTest = /^<[A-Z]+>$/.exec(value);
    if (genericTest) {
        return {
            value,
            type: TreeNodeType.Generic,
            children: [],
        };
    }
    const placeholderTest = /^<([^ <>]+)>$/.exec(value);
    if (placeholderTest) {
        const children = [getTree(placeholderTest[1])];
        return {
            value,
            type: TreeNodeType.Placeholder,
            children,
        };
    }
    if (!ignoreKeyword) {
        const regexTest = /^regex$/.exec(value);
        if (regexTest) {
            return exports.REGEX_TREE;
        }
        const stringTest = /^string$/.exec(value);
        if (stringTest) {
            return exports.STRING_TREE;
        }
        const numberTest = /^number$/.exec(value);
        if (numberTest) {
            return exports.NUMBER_TREE;
        }
        const booleanTest = /^boolean$/.exec(value);
        if (booleanTest) {
            return exports.BOOLEAN_TREE;
        }
        const objectTest = /^object$/.exec(value);
        if (objectTest) {
            return exports.OBJECT_TREE;
        }
        const anyTest = /^any$/.exec(value);
        if (anyTest) {
            return exports.ANY_TREE;
        }
    }
    const propExpressionTest = /^([^ [\]]+)(\[[^ [\]]+\]+)$/.exec(value);
    if (propExpressionTest) {
        const _ = getTree(propExpressionTest[1]);
        const __ = getTree(propExpressionTest[2]);
        const children = [_, __];
        return {
            value,
            type: TreeNodeType.PropExpression,
            children,
        };
    }
    const arrayExpressionTest = /^([^ ]+)\[\]$/.exec(value);
    if (arrayExpressionTest) {
        const child = getTree(arrayExpressionTest[1]);
        if (child.type !== TreeNodeType.Invalid &&
            child.type !== TreeNodeType.Or &&
            child.type !== TreeNodeType.And) {
            const children = [child];
            return {
                value,
                type: TreeNodeType.ArrayExpression,
                children,
            };
        }
    }
    const objectExpressionTest = /^([^|{}]+)\{\}$/.exec(value);
    if (objectExpressionTest) {
        const children = [getTree(objectExpressionTest[1])];
        return {
            value,
            type: TreeNodeType.ObjectExpression,
            children,
        };
    }
    const orTest = /^(?!(['"])[^'"]*\1$).+?\|.+$/.exec(value);
    if (orTest) {
        const children = _getDelimiterSeparated(value, false, false, '|', getTree);
        return {
            value,
            type: TreeNodeType.Or,
            children,
        };
    }
    const andTest = /^(?!(['"])[^'"]*\1$).+?&.+$/.exec(value);
    if (andTest) {
        const children = _getDelimiterSeparated(value, false, false, '&', getTree);
        return {
            value,
            type: TreeNodeType.And,
            children,
        };
    }
    const classTest = /^`([A-Z]+)(.*)?`$/i.exec(value);
    if (classTest) {
        const childStr = classTest[2];
        return {
            value,
            type: TreeNodeType.Class,
            children: childStr ? [getTree(childStr)] : [],
        };
    }
    return undefined;
}
function getValueTree(value, keyValue = false, ignoreKeyword = false, _getTree = _getValueTree) {
    return (_getValueTree(value, keyValue, ignoreKeyword, _getTree) || {
        value,
        type: TreeNodeType.Invalid,
        children: [],
    });
}
function execComposed(str, open_delimiter, close_delimiter) {
    const l = str.length;
    if (str[0] === open_delimiter && str[l - 1] === close_delimiter) {
        let sq_open = false;
        let dq_open = false;
        let open = 0;
        for (let i = 1; i < l - 1; i++) {
            const c = str[i];
            const pc = str[i - 1];
            if (c === "'" && pc !== '\\') {
                if (!dq_open) {
                    sq_open = !sq_open;
                }
            }
            else if (c === '"' && pc !== '\\') {
                if (!sq_open) {
                    dq_open = !dq_open;
                }
            }
            else {
                if (!sq_open && !dq_open) {
                    if (c === open_delimiter) {
                        open++;
                    }
                    else if (c === close_delimiter) {
                        open--;
                        if (open === -1) {
                            return null;
                        }
                    }
                }
            }
        }
        return [str, str.substr(1, l - 2)];
    }
    else {
        return null;
    }
}
function execObject(str) {
    return execComposed(str, exports.OBJECT_OPEN, exports.OBJECT_CLOSE);
}
function execArray(str) {
    return execComposed(str, exports.ARRAY_OPEN, exports.ARRAY_CLOSE);
}
function _getValueTree(value, keyValue = false, ignoreKeyword = false, _getTree = _getValueTree) {
    value = value.trim();
    if (!ignoreKeyword) {
        const nullTest = /^null$/.exec(value);
        if (nullTest) {
            return exports.NULL_TREE;
        }
        const infinityTest = /^[+-]?Infinity$/.exec(value);
        if (infinityTest) {
            return {
                value,
                type: TreeNodeType.NumberLiteral,
                children: [],
            };
        }
    }
    const booleanLiteralTest = BOOLEAN_LITERAL_1.BOOLEAN_LITERAL_REGEX.exec(value);
    if (booleanLiteralTest) {
        return {
            value,
            type: TreeNodeType.BooleanLiteral,
            children: [],
        };
    }
    // const stringLiteralTest = STRING_LITERAL_REGEX.exec(value)
    // if (stringLiteralTest) {
    if ((value.startsWith('"') && value.endsWith('"')) ||
        (value.startsWith("'") && value.endsWith("'"))) {
        const startChar = value[0];
        let valid = true;
        let escaping = false;
        for (let i = 1; i < value.length - 1; i++) {
            if (value[i] === '\\') {
                escaping = !escaping;
            }
            else {
                escaping = false;
            }
            if (value[i] === startChar && value[i - 1] !== '\\') {
                valid = false;
                break;
            }
        }
        if (valid && !escaping) {
            return {
                value,
                type: TreeNodeType.StringLiteral,
                children: [],
            };
        }
    }
    const numberLiteralTest = NUMBER_LITERAL_1.NUMBER_LITERAL_REGEX.exec(value);
    if (numberLiteralTest) {
        return {
            value,
            type: TreeNodeType.NumberLiteral,
            children: [],
        };
    }
    const arithmeticExpressionTest = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?(?:[\s]*[+\-*/][\s]*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)+$/g.exec(value);
    if (arithmeticExpressionTest) {
        return {
            value,
            type: TreeNodeType.ArithmeticExpression,
            children: [],
        };
    }
    const unitTest = /^\$\{(.*)\}$/is.exec(value);
    if (unitTest) {
        return {
            value,
            type: TreeNodeType.Unit,
            children: [],
        };
    }
    const regexLiteralTest = /^\/.+\/$/.exec(value);
    if (regexLiteralTest) {
        return {
            value,
            type: TreeNodeType.RegexLiteral,
            children: [],
        };
    }
    // this check must come after keywords (null, string, etc.)
    if (ignoreKeyword) {
        const identifierTest = IDENTIFIER_1.IDENTIFIER_REGEX.test(value);
        if (identifierTest) {
            return {
                value,
                type: TreeNodeType.Identifier,
                children: [],
            };
        }
    }
    if (keyValue) {
        const keyValueTest = /^("[^"]*"|'[^']*'|[^:{]*):([^]*)$/.exec(value);
        if (keyValueTest) {
            let k = _getTree(keyValueTest[1], false, true);
            const value_value = keyValueTest[2];
            const v = _getTree(value_value) || {
                value: value_value,
                type: TreeNodeType.Invalid,
                children: [],
            };
            const children = [k, v];
            return {
                value,
                type: TreeNodeType.KeyValue,
                children,
            };
        }
    }
    const objectLiteralTest = execObject(value);
    if (objectLiteralTest) {
        const commaSeparated = objectLiteralTest[1];
        const children = _getCommaSeparated(commaSeparated, true, true, _getTree);
        return {
            value,
            type: TreeNodeType.ObjectLiteral,
            children,
        };
    }
    const arrayLiteralTest = execArray(value);
    if (arrayLiteralTest) {
        const commaSeparated = arrayLiteralTest[1];
        const children = _getCommaSeparated(commaSeparated, false, false, _getTree);
        return {
            value,
            type: TreeNodeType.ArrayLiteral,
            children,
        };
    }
    const urlTest = /^unit:\/\/.+$/i.exec(value);
    if (urlTest) {
        return {
            value,
            type: TreeNodeType.Url,
            children: [],
        };
    }
    return undefined;
}
function _extractGenerics(specs, value, type) {
    let generics = {};
    switch (type.type) {
        case TreeNodeType.Generic: {
            const valueTypeTree = _getValueType(specs, value);
            const valueTypeTreeStr = _stringify(valueTypeTree);
            return { [type.value]: valueTypeTreeStr };
        }
        case TreeNodeType.Identifier:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.Object:
        case TreeNodeType.String:
        case TreeNodeType.Number:
        case TreeNodeType.Boolean:
        case TreeNodeType.Any:
        case TreeNodeType.ArithmeticExpression:
            break;
        case TreeNodeType.ArrayLiteral:
        case TreeNodeType.ObjectLiteral: {
            if (value.type === TreeNodeType.ObjectLiteral) {
                const typeKeyValueMap = getObjLiteralKeyValueMap(type);
                const valueKeyValueMap = getObjLiteralKeyValueMap(value);
                for (const key in typeKeyValueMap) {
                    const _key = key.endsWith('?') ? key.slice(0, -1) : key;
                    const childGenerics = _extractGenerics(specs, valueKeyValueMap[_key] ?? valueKeyValueMap[key], typeKeyValueMap[_key] ?? typeKeyValueMap[key]);
                    for (const name in childGenerics) {
                        const childGeneric = childGenerics[name];
                        if (!generics[name]) {
                            generics[name] = childGeneric;
                        }
                        else if (generics[name] !== childGeneric) {
                            throw `Found two possible types for generic ${name}: ${generics[name]} and ${childGeneric}`;
                        }
                    }
                }
            }
            break;
        }
        case TreeNodeType.Or:
        case TreeNodeType.And:
            for (const child of type.children) {
                if (_hasGeneric(child)) {
                    generics = {
                        ...generics,
                        ..._extractGenerics(specs, value, child),
                    };
                }
            }
            break;
        case TreeNodeType.ArrayExpression:
        case TreeNodeType.ObjectExpression:
            return _extractGenerics(specs, value.children[0] || exports.ANY_TREE, type.children[0]);
        case TreeNodeType.Expression:
            // TODO
            break;
        case TreeNodeType.KeyValue:
            return _extractGenerics(specs, value.children[1], type.children[1]);
        case TreeNodeType.Class:
            if (value.type === TreeNodeType.Class ||
                value.type === TreeNodeType.ClassLiteral) {
                const { placeholder: valueTree } = parseClassValue(value);
                return _extractGenerics(specs, valueTree, type.children[0] || exports.ANY_TREE);
            }
            else {
                break;
            }
    }
    return generics;
}
exports.NULL_TREE = {
    value: 'null',
    type: TreeNodeType.Null,
    children: [],
};
exports.STRING_TREE = {
    value: 'string',
    type: TreeNodeType.String,
    children: [],
};
exports.BOOLEAN_TREE = {
    value: 'boolean',
    type: TreeNodeType.Boolean,
    children: [],
};
exports.NUMBER_TREE = {
    value: 'number',
    type: TreeNodeType.Number,
    children: [],
};
exports.ANY_TREE = {
    value: 'any',
    type: TreeNodeType.Any,
    children: [],
};
exports.REGEX_TREE = {
    value: 'regex',
    type: TreeNodeType.Regex,
    children: [],
};
exports.OBJECT_TREE = {
    value: 'object',
    type: TreeNodeType.Object,
    children: [],
};
function basePinsSpecToParams(pins) {
    return (0, f_1.keys)(pins)
        .map((pinId) => {
        const { type } = pins[pinId];
        return `${(0, string_1.removeWhiteSpace)(pinId)}:${type}`;
    })
        .join(',');
}
function _getValueType(specs, tree) {
    let children = [];
    switch (tree.type) {
        case TreeNodeType.Invalid:
        case TreeNodeType.Null:
        case TreeNodeType.Identifier:
            return tree;
        case TreeNodeType.StringLiteral:
            return exports.STRING_TREE;
        case TreeNodeType.BooleanLiteral:
            return exports.BOOLEAN_TREE;
        case TreeNodeType.NumberLiteral:
            return exports.NUMBER_TREE;
        case TreeNodeType.RegexLiteral:
            return exports.REGEX_TREE;
        case TreeNodeType.ClassLiteral: {
            const spec = specs[tree.value];
            return {
                value: `(${basePinsSpecToParams(spec.inputs)})=>(${basePinsSpecToParams(spec.outputs)})`,
                type: TreeNodeType.ClassLiteral,
                children: [],
            };
        }
        case TreeNodeType.ArrayLiteral: {
            const childrenTypes = tree.children.map((c) => _getValueType(specs, c));
            const childrenTypeSet = new Set();
            const childrenTypes_ = childrenTypes
                .map((c) => c.value)
                .filter((value) => {
                if (childrenTypeSet.has(value)) {
                    return false;
                }
                else {
                    childrenTypeSet.add(value);
                    return true;
                }
            });
            const childType = tree.children.length > 0
                ? getTree(childrenTypes.length > 1
                    ? `(${childrenTypes_.join('|')})`
                    : childrenTypes_[0])
                : getTree('<T>');
            return {
                value: `${childType.value}[]`,
                type: TreeNodeType.ArrayExpression,
                children: [childType],
            };
        }
        case TreeNodeType.ObjectLiteral:
            children = tree.children.map((c) => _getValueType(specs, c));
            return {
                value: `{${children.map((c) => c.value).join(',')}}`,
                type: TreeNodeType.ObjectLiteral,
                children,
            };
        case TreeNodeType.KeyValue: {
            const key = tree.children[0];
            const value = _getValueType(specs, tree.children[1]);
            children = [key, value];
            return {
                value: `${key.value}:${value.value}`,
                type: TreeNodeType.KeyValue,
                children,
            };
        }
        case TreeNodeType.Unit: {
            const { value } = tree;
            const bundle = (0, idFromUnitValue_1.evaluateBundleStr)(value, specs, {}); // TODO
            const specId = (0, idFromUnitValue_1.idFromUnitValue)(value, specs, {}); // TODO
            const spec = specs[specId] || bundle.specs[specId];
            if (!spec) {
                throw new Error('Spec not found: ' + specId);
            }
            const { type = '`U`&`C`&`G`' } = spec;
            return getTree(type);
        }
        default:
            return tree;
    }
}
function getValueType(specs, value) {
    const valueTree = getTree(value);
    const typeTree = _getValueType(specs, valueTree);
    return typeTree.value;
}
function _stringify(tree) {
    switch (tree.type) {
        case TreeNodeType.Invalid:
        case TreeNodeType.Null:
        case TreeNodeType.Generic:
        case TreeNodeType.Identifier:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.Object:
        case TreeNodeType.String:
        case TreeNodeType.Number:
        case TreeNodeType.Any:
        case TreeNodeType.Boolean:
            return tree.value;
        case TreeNodeType.ArrayLiteral: {
            const children = tree.children;
            return `[${children.map(_stringify).join(',')}]`;
        }
        case TreeNodeType.ObjectLiteral:
            return `{${tree.children.map(_stringify).join(',')}}`;
        case TreeNodeType.Or:
        case TreeNodeType.And:
            return tree.value;
        case TreeNodeType.ArrayExpression:
            return `${tree.children[0].value}[]`;
        case TreeNodeType.ObjectExpression:
            return `${tree.children[0].value}{}`;
        case TreeNodeType.Expression:
            // TODO
            return '';
        case TreeNodeType.KeyValue:
            return `${(tree.children[0] && _stringify(tree.children[0])) || ''}${(!!tree.children[1] && `:${_stringify(tree.children[1])}`) || ''}`;
        default:
            return tree.value;
    }
}
function _filterEmptyNodes(tree, _getTree = getTree) {
    if (isCompositeType(tree.type)) {
        let totalCount = 0;
        const children = tree.children.map((child) => {
            const [childTree, childCount] = _filterEmptyNodes(child, _getTree);
            totalCount += childCount;
            return childTree;
        });
        const children_ = children.filter((c) => {
            if (c.value) {
                return true;
            }
            else {
                totalCount++;
                return false;
            }
        });
        if (totalCount === 0) {
            return [tree, 0];
        }
        const value = _stringify({
            ...tree,
            children: children_,
        });
        const filteredTree = _getTree(value, true);
        return [filteredTree, totalCount];
    }
    else {
        return [tree, 0];
    }
}
function filterEmptyNodes(value) {
    const tree = getTree(value, true);
    return _filterEmptyNodes(tree)[0];
}
function findGenerics(value) {
    const tree = getTree(value);
    return _findGenerics(tree);
}
function _findGenerics(tree) {
    if (tree.type === TreeNodeType.Generic) {
        return new Set([tree.value]);
    }
    else {
        return tree.children.reduce((acc, c) => new Set([...acc, ..._findGenerics(c)]), new Set([]));
    }
}
function extractGenerics(specs, value, type) {
    const valueTree = getTree(value);
    const typeTree = getTree(type);
    return _extractGenerics(specs, valueTree, typeTree);
}
function _applyGenerics(tree, map) {
    switch (tree.type) {
        case TreeNodeType.Generic: {
            const replacement = map[tree.value];
            if (replacement) {
                const replacementTree = getTree(replacement);
                return {
                    value: replacement,
                    type: replacementTree.type,
                    children: replacementTree.children,
                };
            }
            else {
                return tree;
            }
        }
        case TreeNodeType.Invalid:
        case TreeNodeType.Identifier:
        case TreeNodeType.StringLiteral:
        case TreeNodeType.BooleanLiteral:
        case TreeNodeType.NumberLiteral:
        case TreeNodeType.Null:
        case TreeNodeType.Object:
        case TreeNodeType.Any:
        case TreeNodeType.String:
        case TreeNodeType.Unit:
        case TreeNodeType.Number:
        case TreeNodeType.Boolean:
            return tree;
        case TreeNodeType.ObjectLiteral:
        case TreeNodeType.ArrayLiteral:
        case TreeNodeType.Or:
        case TreeNodeType.And:
        case TreeNodeType.ArrayExpression:
        case TreeNodeType.ObjectExpression:
        case TreeNodeType.Expression:
        case TreeNodeType.KeyValue:
        case TreeNodeType.ClassLiteral:
        case TreeNodeType.PropExpression: {
            const children = tree.children.map((child) => _applyGenerics(child, map));
            const value = tree.children.reduce((acc, child, i) => {
                return acc.replace(child.value, children[i].value);
            }, tree.value);
            return {
                ...tree,
                value,
                children,
            };
        }
        case TreeNodeType.Class: {
            const children = tree.children.map((child) => child.type === TreeNodeType.Generic ? _applyGenerics(child, map) : child);
            const value = tree.children.reduce((acc, child, i) => {
                const child_ = children[i];
                if (child.type === TreeNodeType.Generic) {
                    return acc.replace(trimSides(child.value), child_.type === TreeNodeType.Generic
                        ? trimSides(child_.value)
                        : child_.value);
                }
                else {
                    return tree.value;
                }
            }, tree.value);
            return {
                ...tree,
                value,
                children,
            };
        }
        default:
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
    }
}
function applyGenerics(type, map) {
    const typeTree = getTree(type);
    const valueTree = _applyGenerics(typeTree, map);
    const { value } = valueTree;
    return value;
}
function _isEmptyString(str) {
    return !!/^ *$/.exec(str);
}
function _getDelimiterSeparated(value, keyValue = false, ignoreKeyword = false, delimiter, _getTree) {
    value = value.trim();
    let objectOpenCount = 0;
    let arrayOpenCount = 0;
    let parenthesisOpenCount = 0;
    let singleQuoteOpen = false;
    let doubleQuoteOpen = false;
    let pos = 0;
    let lastStop = 0;
    let pushNext = false;
    const children = [];
    let prevChar = undefined;
    let escaping = false;
    while (pos < value.length) {
        const char = value[pos];
        if (!doubleQuoteOpen && !singleQuoteOpen) {
            if (char === exports.OBJECT_OPEN) {
                objectOpenCount++;
            }
            else if (char === exports.OBJECT_CLOSE) {
                objectOpenCount--;
            }
            if (char === exports.ARRAY_OPEN) {
                arrayOpenCount++;
            }
            else if (char === exports.ARRAY_CLOSE) {
                arrayOpenCount--;
            }
            if (char === exports.PARENTHESIS_OPEN) {
                parenthesisOpenCount++;
            }
            else if (char === exports.PARENTHESIS_CLOSE) {
                parenthesisOpenCount--;
            }
        }
        if (char === '\\') {
            escaping = !escaping;
        }
        if (!escaping) {
            if (!singleQuoteOpen && char === exports.DOUBLE_QUOTE) {
                doubleQuoteOpen = !doubleQuoteOpen;
            }
            if (!doubleQuoteOpen && char === exports.SINGLE_QUOTE) {
                singleQuoteOpen = !singleQuoteOpen;
            }
        }
        if (char !== '\\') {
            escaping = false;
        }
        if (char === delimiter &&
            objectOpenCount === 0 &&
            arrayOpenCount === 0 &&
            parenthesisOpenCount === 0 &&
            !singleQuoteOpen &&
            !doubleQuoteOpen) {
            const childString = value.substring(lastStop, pos);
            const tree = _getTree(childString, keyValue, ignoreKeyword);
            children.push(tree);
            lastStop = pos + 1;
            pushNext = true;
        }
        prevChar = char;
        pos++;
    }
    // push last element
    const lastChildString = value.substring(lastStop, value.length);
    if (!_isEmptyString(lastChildString) || pushNext) {
        children.push(_getTree(lastChildString, keyValue, ignoreKeyword));
    }
    return children;
}
function _getCommaSeparated(value, keyValue = false, ignoreKeyword = false, _getTree) {
    return _getDelimiterSeparated(value, keyValue, ignoreKeyword, exports.COMMA, _getTree);
}
function _getNodeAtPath(root, path) {
    let cursor = root;
    for (const key of path) {
        if (cursor) {
            cursor = cursor.children[key];
        }
        else {
            return null;
        }
    }
    return cursor;
}
function getNodeAtPath(data, path) {
    const tree = getTree(data);
    const node = _getNodeAtPath(tree, path);
    return (node && node.value) || null;
}
function _getNextNode(node, path, direction) {
    const nextNodePath = _getNextNodePath(node, path, direction);
    return nextNodePath && _getNodeAtPath(node, nextNodePath);
}
function getNextNode(data, path, direction) {
    const tree = getTree(data);
    const nextNode = _getNextNode(tree, path, direction);
    return nextNode && nextNode.value;
}
function _getNodePaths(root) {
    const paths = [];
    traverseTree(root, (node, path) => {
        paths.push(path);
    });
    return paths;
}
function _getNodesAndPaths(root) {
    const paths = [];
    traverseTree(root, (node, path) => {
        paths.push({ node, path });
    });
    return paths;
}
function _pathEqual(a, b) {
    if (a.length !== b.length) {
        return false;
    }
    for (let i = 0; i < a.length; i++) {
        if (a[i] !== b[i]) {
            return false;
        }
    }
    return true;
}
// Given a path and a direction, return the next path in preorder order traversal
// f("[0, 1, 2]", [1], 1) === [2]
// f("{ a: { b: "c" }, d: "f" }", [], 1) === [0]
// f("{ a: { b: "c" }, d: "f" }", [0, 1, 0, 1], 1) === [1]
function _getNextNodePath(root, path, direction) {
    const paths = _getNodePaths(root);
    for (let i = 0; i < paths.length; i++) {
        const p = paths[i];
        if (_pathEqual(path, p)) {
            return paths[i + direction];
        }
    }
    return null;
}
function getNextNodePath(data, path, direction) {
    const tree = getTree(data);
    return _getNextNodePath(tree, path, direction);
}
function _getNextLeafPath(root, path, direction) {
    const nodesAndPaths = _getNodesAndPaths(root);
    for (let i = 0; i < nodesAndPaths.length; i++) {
        const { path: p } = nodesAndPaths[i];
        if (_pathEqual(path, p)) {
            for (let j = i + direction; j >= 0 && j < nodesAndPaths.length; j = j + direction) {
                const { node, path: p } = nodesAndPaths[j];
                if (node.children.length === 0) {
                    return p;
                }
            }
        }
    }
    return null;
}
function getNextLeafPath(data, path, direction) {
    const tree = getTree(data);
    return _getNextLeafPath(tree, path, direction);
}
function _getNextSiblingPath(tree, path, direction) {
    return _getNextLeafPath(tree, path, direction);
}
function getNextSiblingPath(value, path, direction) {
    const tree = getTree(value);
    return _getNextLeafPath(tree, path, direction);
}
function getParentPath(path) {
    if (path.length === 0) {
        return null;
    }
    return path.slice(0, path.length - 1);
}
function getParent(root, path) {
    const tree = getTree(root);
    const parent = _getParent(tree, path);
    if (!parent) {
        return null;
    }
    return parent.value;
}
function _getLastLeaf(root) {
    let cursor = root;
    while (cursor.children.length > 0) {
        const last_child = cursor.children[cursor.children.length - 1];
        cursor = last_child;
    }
    return cursor;
}
function _getLastLeafPath(root) {
    let path = [];
    let cursor = root;
    let l = cursor.children.length;
    while (l > 0) {
        const last_child_index = l - 1;
        path.push(last_child_index);
        const last_child = cursor.children[last_child_index];
        cursor = last_child;
        l = cursor.children.length;
    }
    return path;
}
function getLastLeafPath(root) {
    const tree = getTree(root);
    const path = _getLastLeafPath(tree);
    return path;
}
function getLastLeaf(root) {
    const tree = getTree(root);
    const last = _getLastLeaf(tree);
    const { value } = last;
    return value;
}
function _getParent(root, path) {
    const parentPath = getParentPath(path);
    if (!parentPath) {
        return null;
    }
    return _getNodeAtPath(root, parentPath);
}
function _insertNodeAt(root, path, node) {
    if (path.length === 0) {
        return node;
    }
    root = (0, clone_1.clone)(root);
    const parentPath = getParentPath(path);
    const parentNode = _getNodeAtPath(root, parentPath);
    const last = path[path.length - 1];
    parentNode.children.splice(last, 0, node);
    let i = 0;
    let n = root;
    do {
        n.value = _stringify(n);
        n = n.children[path[i]];
        i++;
    } while (i < path.length - 1);
    // TODO performance
    return getTree(root.value);
}
function insertNodeAt(root, path, node) {
    const rootTree = getTree(root);
    const nodeTree = getTree(node);
    const updatedRoot = _insertNodeAt(rootTree, path, nodeTree);
    return updatedRoot.value;
}
function _updateNodeAt(root, path, update) {
    if (path.length === 0) {
        return update;
    }
    root = (0, clone_1.clone)(root);
    const parentPath = getParentPath(path);
    const parentNode = _getNodeAtPath(root, parentPath);
    const last = path[path.length - 1];
    parentNode.children[last] = update;
    let i = 0;
    let n = root;
    do {
        n.value = _stringify(n);
        n = n.children[path[i]];
        i++;
    } while (i < path.length - 1);
    // TODO performance
    return getTree(root.value);
}
function updateNodeAt(root, path, update) {
    const rootTree = getTree(root);
    const updateTree = getTree(update);
    const updatedRoot = _updateNodeAt(rootTree, path, updateTree);
    return updatedRoot.value;
}
exports.EMPTY_TREE = getTree('');
function _removeNodeAt(root, path) {
    if (path.length === 0) {
        return exports.EMPTY_TREE;
    }
    const parentPath = getParentPath(path);
    const parentNode = _getNodeAtPath(root, parentPath);
    const nodeIndex = path[path.length - 1];
    const updatedParentNode = (0, clone_1.clone)(parentNode);
    updatedParentNode.children.splice(nodeIndex, 1);
    return _updateNodeAt(root, parentPath, getTree(_stringify(updatedParentNode)));
}
function removeNodeAt(root, path) {
    const rootTree = getTree(root);
    const updatedRoot = _removeNodeAt(rootTree, path);
    return updatedRoot.value;
}
function _matchAllExcTypes(specs, a, b) {
    return (0, array_1.matchAllExc)(a, b, (a, b) => _isTypeMatch(specs, a, b));
}
function matchAllExcTypes(specs, a, b) {
    return (0, array_1.matchAllExc)(a, b, (a, b) => isTypeMatch(specs, a, b));
}
function isUnitNodePredicate(node, path) {
    return node.type === TreeNodeType.Unit;
}
function findAllUnitNodes(tree) {
    return findAllNodes(tree, isUnitNodePredicate);
}
function findAndReplaceUnitNodes_(tree) {
    const paths = [];
    let newTree = (0, clone_1.clone)(tree);
    traverseTree(tree, (node, path) => {
        if (isUnitNodePredicate(node, path)) {
            const str = node.value.substring(1);
            const [ref, replacedTree] = findAndReplaceUnitNodes_(getTree(str));
            paths.push(path);
            for (const r in ref) {
                paths.push([...path, ...r]);
            }
            newTree = _updateNodeAt(newTree, path, replacedTree);
        }
    });
    return [paths, newTree];
}
function findAndReplaceUnitNodes(value) {
    const tree = getTree(value);
    return findAndReplaceUnitNodes_(tree);
}
function findAllNodes(tree, predicate) {
    const paths = [];
    traverseTree(tree, (node, path) => {
        if (predicate(node, path)) {
            paths.push(path);
        }
    });
    return paths;
}
// export function getTreeFromRawData(data: any): TreeNode {
//   const t = typeof data
//   switch (t) {
//     case 'string':
//       return {
//         value: `"${escape(data)}"`,
//         children: [],
//         type: TreeNodeType.BooleanLiteral,
//       }
//     case 'boolean':
//       return {
//         value: `${data}`,
//         children: [],
//         type: TreeNodeType.BooleanLiteral,
//       }
//   }
// }
//# sourceMappingURL=parser.js.map