import isArray_default from "../../../../lodash-es/isArray.js";
import isEmpty_default from "../../../../lodash-es/isEmpty.js";
import flatten_default from "../../../../lodash-es/flatten.js";
import clone_default from "../../../../lodash-es/clone.js";
import compact_default from "../../../../lodash-es/compact.js";
import difference_default from "../../../../lodash-es/difference.js";
import forEach_default from "../../../../lodash-es/forEach.js";
import map_default from "../../../../lodash-es/map.js";
import has_default from "../../../../lodash-es/has.js";
import includes_default from "../../../../lodash-es/includes.js";
function tokenStructuredMatcher(e, y) {
	let b = e.tokenTypeIdx;
	return b === y.tokenTypeIdx ? !0 : y.isParent === !0 && y.categoryMatchesMap[b] === !0;
}
function tokenStructuredMatcherNoCategories(e, y) {
	return e.tokenTypeIdx === y.tokenTypeIdx;
}
let tokenShortNameIdx = 1;
const tokenIdxToClass = {};
function augmentTokenTypes(e) {
	let y = expandCategories(e);
	assignTokenDefaultProps(y), assignCategoriesMapProp(y), assignCategoriesTokensProp(y), forEach_default(y, (e) => {
		e.isParent = e.categoryMatches.length > 0;
	});
}
function expandCategories(e) {
	let S = clone_default(e), C = e, w = !0;
	for (; w;) {
		C = compact_default(flatten_default(map_default(C, (e) => e.CATEGORIES)));
		let e = difference_default(C, S);
		S = S.concat(e), isEmpty_default(e) ? w = !1 : C = e;
	}
	return S;
}
function assignTokenDefaultProps(y) {
	forEach_default(y, (y) => {
		hasShortKeyProperty(y) || (tokenIdxToClass[tokenShortNameIdx] = y, y.tokenTypeIdx = tokenShortNameIdx++), hasCategoriesProperty(y) && !isArray_default(y.CATEGORIES) && (y.CATEGORIES = [y.CATEGORIES]), hasCategoriesProperty(y) || (y.CATEGORIES = []), hasExtendingTokensTypesProperty(y) || (y.categoryMatches = []), hasExtendingTokensTypesMapProperty(y) || (y.categoryMatchesMap = {});
	});
}
function assignCategoriesTokensProp(e) {
	forEach_default(e, (e) => {
		e.categoryMatches = [], forEach_default(e.categoryMatchesMap, (y, b) => {
			e.categoryMatches.push(tokenIdxToClass[b].tokenTypeIdx);
		});
	});
}
function assignCategoriesMapProp(e) {
	forEach_default(e, (e) => {
		singleAssignCategoriesToksMap([], e);
	});
}
function singleAssignCategoriesToksMap(e, y) {
	forEach_default(e, (e) => {
		y.categoryMatchesMap[e.tokenTypeIdx] = !0;
	}), forEach_default(y.CATEGORIES, (b) => {
		let x = e.concat(y);
		includes_default(x, b) || singleAssignCategoriesToksMap(x, b);
	});
}
function hasShortKeyProperty(e) {
	return has_default(e, "tokenTypeIdx");
}
function hasCategoriesProperty(e) {
	return has_default(e, "CATEGORIES");
}
function hasExtendingTokensTypesProperty(e) {
	return has_default(e, "categoryMatches");
}
function hasExtendingTokensTypesMapProperty(e) {
	return has_default(e, "categoryMatchesMap");
}
function isTokenType(e) {
	return has_default(e, "tokenTypeIdx");
}
export { augmentTokenTypes, hasShortKeyProperty, isTokenType, tokenStructuredMatcher, tokenStructuredMatcherNoCategories };
