"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__, {
    renderQuickviewIframe: ()=>renderQuickviewIframe
});
const external_lit_namespaceObject = require("lit");
const if_defined_js_namespaceObject = require("lit/directives/if-defined.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const event_utils_cjs_namespaceObject = require("../../../../utils/event-utils.cjs");
const documentIdentifierInIframe = 'CoveoDocIdentifier';
const writeDocument = (documentWriter, content)=>{
    documentWriter.open();
    documentWriter.write(content);
    documentWriter.close();
    if (documentWriter.scrollingElement) documentWriter.scrollingElement.scrollTop = 0;
};
const currentResultAlreadyWrittenToDocument = (documentWriter, uniqueIdentifier)=>{
    const currentDocIdentifier = documentWriter.getElementById(documentIdentifierInIframe);
    return currentDocIdentifier?.textContent === uniqueIdentifier;
};
const ensureSameResultIsNotOverwritten = (documentWriter, uniqueIdentifier)=>{
    const docIdentifier = documentWriter.createElement('div');
    docIdentifier.style.display = 'none';
    docIdentifier.setAttribute('aria-hidden', 'true');
    docIdentifier.id = documentIdentifierInIframe;
    docIdentifier.textContent = uniqueIdentifier;
    documentWriter.body.appendChild(docIdentifier);
};
const warnAboutLimitedUsageQuickview = (logger)=>{
    logger?.warn('Quickview initialized in restricted mode due to incompatible sandboxing environment. Keywords hit navigation will be disabled.');
};
const iframeConnectedEvent = async (iframe, logger)=>{
    if (iframe.isConnected) return;
    const timeoutMs = 1000;
    await (0, event_utils_cjs_namespaceObject.eventPromise)(iframe, 'load', timeoutMs).catch(()=>{
        logger?.warn('Quickview timed out waiting for the iframe to load.');
    });
};
const renderQuickviewIframe = ({ props })=>{
    const { title, onSetIframeRef, uniqueIdentifier, content, sandbox, src, logger } = props;
    const waitForIframeContentToBeWritten = ()=>new Promise((resolve)=>setTimeout(resolve));
    const handleRef = (el)=>{
        if (!el) return;
        const iframeRef = el;
        if (!uniqueIdentifier || !content) return;
        const writeContentWhenReady = async ()=>{
            await iframeConnectedEvent(iframeRef, logger);
            const documentWriter = iframeRef.contentDocument;
            if (!documentWriter) {
                if (src) {
                    warnAboutLimitedUsageQuickview(logger);
                    iframeRef.src = src;
                }
                return;
            }
            if (currentResultAlreadyWrittenToDocument(documentWriter, uniqueIdentifier)) return;
            writeDocument(documentWriter, content);
            ensureSameResultIsNotOverwritten(documentWriter, uniqueIdentifier);
            await waitForIframeContentToBeWritten();
            onSetIframeRef(iframeRef);
        };
        writeContentWhenReady();
    };
    return (0, external_lit_namespaceObject.html)`<iframe
    title=${title}
    src="about:blank"
    class="h-full w-full"
    sandbox=${(0, if_defined_js_namespaceObject.ifDefined)(sandbox)}
    ${(0, ref_js_namespaceObject.ref)(handleRef)}
  ></iframe>`;
};
exports.renderQuickviewIframe = __webpack_exports__.renderQuickviewIframe;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "renderQuickviewIframe"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
