"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__, {
    bindLogDocumentOpenOnResult: ()=>bindLogDocumentOpenOnResult,
    buildStringTemplateFromResult: ()=>buildStringTemplateFromResult,
    getStringValueFromResultOrNull: ()=>getStringValueFromResultOrNull
});
const headless_namespaceObject = require("@coveo/headless");
const external_object_utils_cjs_namespaceObject = require("./object-utils.cjs");
function bindLogDocumentOpenOnResult(engine, result, resultElement, selector) {
    const interactiveResult = (0, headless_namespaceObject.buildInteractiveResult)(engine, {
        options: {
            result
        }
    });
    const eventsMap = {
        contextmenu: ()=>interactiveResult.select(),
        click: ()=>interactiveResult.select(),
        mouseup: ()=>interactiveResult.select(),
        mousedown: ()=>interactiveResult.select(),
        touchstart: ()=>interactiveResult.beginDelayedSelect(),
        touchend: ()=>interactiveResult.cancelPendingSelect()
    };
    const elements = resultElement.querySelectorAll(selector || 'a');
    elements.forEach((element)=>{
        Object.keys(eventsMap).forEach((key)=>element.addEventListener(key, eventsMap[key]));
    });
    return ()=>{
        elements.forEach((element)=>{
            Object.keys(eventsMap).forEach((key)=>element.removeEventListener(key, eventsMap[key]));
        });
    };
}
function buildStringTemplateFromResult(template, result, bindings) {
    return template.replace(/\${(.*?)}/g, (value)=>{
        const key = value.substring(2, value.length - 1);
        let newValue = (0, external_object_utils_cjs_namespaceObject.readFromObject)(result, key);
        if (!newValue && "u" > typeof window) newValue = (0, external_object_utils_cjs_namespaceObject.readFromObject)(window, key);
        if (!newValue) {
            bindings.engine.logger.warn(`${key} used in the href template is undefined for this result: ${result.uniqueId} and could not be found in the window object.`);
            return '';
        }
        return newValue;
    });
}
function getStringValueFromResultOrNull(result, field) {
    const value = headless_namespaceObject.ResultTemplatesHelpers.getResultProperty(result, field);
    if ('string' != typeof value || '' === value.trim()) return null;
    return value;
}
exports.bindLogDocumentOpenOnResult = __webpack_exports__.bindLogDocumentOpenOnResult;
exports.buildStringTemplateFromResult = __webpack_exports__.buildStringTemplateFromResult;
exports.getStringValueFromResultOrNull = __webpack_exports__.getStringValueFromResultOrNull;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "bindLogDocumentOpenOnResult",
    "buildStringTemplateFromResult",
    "getStringValueFromResultOrNull"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
