'use strict';

var forEach = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/forEach.js');
var clone = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/clone.js');
var compact = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/compact.js');
var flatten = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/flatten.js');
var map = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/map.js');
var difference = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/difference.js');
var isEmpty = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isEmpty.js');
var isArray = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/isArray.js');
var has = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/has.js');
var includes = require('../../../../../../lodash-es@4.17.21/node_modules/lodash-es/includes.js');

function tokenStructuredMatcher(tokInstance, tokConstructor) {
    const instanceType = tokInstance.tokenTypeIdx;
    if (instanceType === tokConstructor.tokenTypeIdx) {
        return true;
    }
    else {
        return (tokConstructor.isParent === true &&
            tokConstructor.categoryMatchesMap[instanceType] === true);
    }
}
// Optimized tokenMatcher in case our grammar does not use token categories
// Being so tiny it is much more likely to be in-lined and this avoid the function call overhead
function tokenStructuredMatcherNoCategories(token, tokType) {
    return token.tokenTypeIdx === tokType.tokenTypeIdx;
}
exports.tokenShortNameIdx = 1;
const tokenIdxToClass = {};
function augmentTokenTypes(tokenTypes) {
    // collect the parent Token Types as well.
    const tokenTypesAndParents = expandCategories(tokenTypes);
    // add required tokenType and categoryMatches properties
    assignTokenDefaultProps(tokenTypesAndParents);
    // fill up the categoryMatches
    assignCategoriesMapProp(tokenTypesAndParents);
    assignCategoriesTokensProp(tokenTypesAndParents);
    forEach.default(tokenTypesAndParents, (tokType) => {
        tokType.isParent = tokType.categoryMatches.length > 0;
    });
}
function expandCategories(tokenTypes) {
    let result = clone.default(tokenTypes);
    let categories = tokenTypes;
    let searching = true;
    while (searching) {
        categories = compact.default(flatten.default(map.default(categories, (currTokType) => currTokType.CATEGORIES)));
        const newCategories = difference.default(categories, result);
        result = result.concat(newCategories);
        if (isEmpty.default(newCategories)) {
            searching = false;
        }
        else {
            categories = newCategories;
        }
    }
    return result;
}
function assignTokenDefaultProps(tokenTypes) {
    forEach.default(tokenTypes, (currTokType) => {
        if (!hasShortKeyProperty(currTokType)) {
            tokenIdxToClass[exports.tokenShortNameIdx] = currTokType;
            currTokType.tokenTypeIdx = exports.tokenShortNameIdx++;
        }
        // CATEGORIES? : TokenType | TokenType[]
        if (hasCategoriesProperty(currTokType) &&
            !isArray.default(currTokType.CATEGORIES)
        // &&
        // !isUndefined(currTokType.CATEGORIES.PATTERN)
        ) {
            currTokType.CATEGORIES = [currTokType.CATEGORIES];
        }
        if (!hasCategoriesProperty(currTokType)) {
            currTokType.CATEGORIES = [];
        }
        if (!hasExtendingTokensTypesProperty(currTokType)) {
            currTokType.categoryMatches = [];
        }
        if (!hasExtendingTokensTypesMapProperty(currTokType)) {
            currTokType.categoryMatchesMap = {};
        }
    });
}
function assignCategoriesTokensProp(tokenTypes) {
    forEach.default(tokenTypes, (currTokType) => {
        // avoid duplications
        currTokType.categoryMatches = [];
        forEach.default(currTokType.categoryMatchesMap, (val, key) => {
            currTokType.categoryMatches.push(tokenIdxToClass[key].tokenTypeIdx);
        });
    });
}
function assignCategoriesMapProp(tokenTypes) {
    forEach.default(tokenTypes, (currTokType) => {
        singleAssignCategoriesToksMap([], currTokType);
    });
}
function singleAssignCategoriesToksMap(path, nextNode) {
    forEach.default(path, (pathNode) => {
        nextNode.categoryMatchesMap[pathNode.tokenTypeIdx] = true;
    });
    forEach.default(nextNode.CATEGORIES, (nextCategory) => {
        const newPath = path.concat(nextNode);
        // avoids infinite loops due to cyclic categories.
        if (!includes.default(newPath, nextCategory)) {
            singleAssignCategoriesToksMap(newPath, nextCategory);
        }
    });
}
function hasShortKeyProperty(tokType) {
    return has.default(tokType, "tokenTypeIdx");
}
function hasCategoriesProperty(tokType) {
    return has.default(tokType, "CATEGORIES");
}
function hasExtendingTokensTypesProperty(tokType) {
    return has.default(tokType, "categoryMatches");
}
function hasExtendingTokensTypesMapProperty(tokType) {
    return has.default(tokType, "categoryMatchesMap");
}
function isTokenType(tokType) {
    return has.default(tokType, "tokenTypeIdx");
}

exports.assignCategoriesMapProp = assignCategoriesMapProp;
exports.assignCategoriesTokensProp = assignCategoriesTokensProp;
exports.assignTokenDefaultProps = assignTokenDefaultProps;
exports.augmentTokenTypes = augmentTokenTypes;
exports.expandCategories = expandCategories;
exports.hasCategoriesProperty = hasCategoriesProperty;
exports.hasExtendingTokensTypesMapProperty = hasExtendingTokensTypesMapProperty;
exports.hasExtendingTokensTypesProperty = hasExtendingTokensTypesProperty;
exports.hasShortKeyProperty = hasShortKeyProperty;
exports.isTokenType = isTokenType;
exports.singleAssignCategoriesToksMap = singleAssignCategoriesToksMap;
exports.tokenIdxToClass = tokenIdxToClass;
exports.tokenStructuredMatcher = tokenStructuredMatcher;
exports.tokenStructuredMatcherNoCategories = tokenStructuredMatcherNoCategories;
