"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const forEach = require("../lodash-es/forEach.cjs");
const clone = require("../lodash-es/clone.cjs");
const compact = require("../lodash-es/compact.cjs");
const flatten = require("../lodash-es/flatten.cjs");
const map = require("../lodash-es/map.cjs");
const difference = require("../lodash-es/difference.cjs");
const isEmpty = require("../lodash-es/isEmpty.cjs");
const isArray = require("../lodash-es/isArray.cjs");
const has = require("../lodash-es/has.cjs");
const includes = require("../lodash-es/includes.cjs");
function tokenStructuredMatcher(tokInstance, tokConstructor) {
  const instanceType = tokInstance.tokenTypeIdx;
  if (instanceType === tokConstructor.tokenTypeIdx) {
    return true;
  } else {
    return tokConstructor.isParent === true && tokConstructor.categoryMatchesMap[instanceType] === true;
  }
}
function tokenStructuredMatcherNoCategories(token, tokType) {
  return token.tokenTypeIdx === tokType.tokenTypeIdx;
}
exports.tokenShortNameIdx = 1;
const tokenIdxToClass = {};
function augmentTokenTypes(tokenTypes) {
  const tokenTypesAndParents = expandCategories(tokenTypes);
  assignTokenDefaultProps(tokenTypesAndParents);
  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++;
    }
    if (hasCategoriesProperty(currTokType) && !isArray.default(currTokType.CATEGORIES)) {
      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) => {
    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);
    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;
//# sourceMappingURL=lib-src-scan-tokens.cjs.map
