class TextParser {
    parse(element) {
        const innerTextOfHTMLElement = this.getHighlightedInnerText(element);
        return this.resolveOriginalTerm(innerTextOfHTMLElement).trim();
    }
    isTaggedWord(element) {
        return 'coveotaggedword' === element.nodeName.toLowerCase();
    }
    getHighlightedInnerText(element) {
        if (!this.isTaggedWord(element)) return this.getTextOfHTMLElement(element);
        const children = Array.from(element.children);
        if (children.length >= 1) return this.getTextOfHTMLElement(children[0]);
        return '';
    }
    getTextOfHTMLElement(el) {
        return el.innerText || el.textContent || '';
    }
    resolveOriginalTerm(highlight) {
        const found = Object.keys(this.stemmingInfoFromIndex).find((originalTerm)=>{
            const originalTermMatch = originalTerm.toLowerCase() === highlight.toLowerCase();
            if (originalTermMatch) return true;
            const stemmingExpansions = this.stemmingInfoFromIndex[originalTerm];
            if (!stemmingExpansions) return false;
            const stemmingExpansionMatch = stemmingExpansions.find((stemmingExpansion)=>stemmingExpansion.toLowerCase() === highlight.toLowerCase());
            return stemmingExpansionMatch;
        });
        return found || highlight;
    }
    constructor(stemmingInfoFromIndex){
        this.stemmingInfoFromIndex = stemmingInfoFromIndex;
    }
}
export { TextParser };
