"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    HIGHLIGHT_PREFIX: ()=>HIGHLIGHT_PREFIX,
    getWordsHighlights: ()=>getWordsHighlights
});
const external_quickview_word_highlight_cjs_namespaceObject = require("./quickview-word-highlight.cjs");
const external_text_parser_cjs_namespaceObject = require("./text-parser.cjs");
const HIGHLIGHT_PREFIX = 'CoveoHighlight';
function findHighlightedElements(iframe) {
    const root = iframe?.contentDocument?.body;
    if (!root) return [];
    const selector = `[id^="${HIGHLIGHT_PREFIX}"]`;
    const query = root.querySelectorAll(selector);
    return Array.from(query);
}
class IframeParser {
    parse() {
        const elements = findHighlightedElements(this.iframe);
        for (const element of elements){
            const identifier = this.parseIdentifier(element);
            if (!identifier) throw new Error('Invalid keyword identifier for quickview');
            const text = this.textParser.parse(element);
            if (!text) continue;
            const alreadyScannedKeyword = this.wordsHighlights[identifier];
            if (alreadyScannedKeyword) {
                alreadyScannedKeyword.addElement(element);
                continue;
            }
            const color = element.style.backgroundColor;
            const highlight = new external_quickview_word_highlight_cjs_namespaceObject.QuickviewWordHighlight(identifier, text, color, element);
            this.wordsHighlights[identifier] = highlight;
        }
        return this.wordsHighlights;
    }
    parseIdentifier(element) {
        const parts = element.id.substring(HIGHLIGHT_PREFIX.length + 1).match(/^([0-9]+)\.([0-9]+)\.([0-9]+)$/);
        if (!parts || parts.length <= 3) return null;
        return parts[1];
    }
    constructor(stemmingInfoFromIndex, iframe){
        this.stemmingInfoFromIndex = stemmingInfoFromIndex;
        this.iframe = iframe;
        this.textParser = new external_text_parser_cjs_namespaceObject.TextParser(this.stemmingInfoFromIndex);
        this.wordsHighlights = {};
    }
}
const getWordsHighlights = (stemmingInfoFromIndex, iframe)=>{
    const parser = new IframeParser(stemmingInfoFromIndex, iframe);
    return parser.parse();
};
exports.HIGHLIGHT_PREFIX = __webpack_exports__.HIGHLIGHT_PREFIX;
exports.getWordsHighlights = __webpack_exports__.getWordsHighlights;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "HIGHLIGHT_PREFIX",
    "getWordsHighlights"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
