"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const domUtils_1 = require("../../sharedUtils/domUtils");
const BaseContentEventHandler_1 = require("./BaseContentEventHandler");
class OverlayHandler extends BaseContentEventHandler_1.BaseContentEventHandler {
    constructor(overlayText = '', store, selectorCache, name = 'Overlay Handler', requestAttention = false, overlayClassname, overlaySubtext, hideInIframe = false, addHighlightBox = true) {
        super(undefined, name, store, selectorCache);
        this.overlayText = overlayText;
        this.overlaySubtext = overlaySubtext;
        this.handleScroll = this.handleScroll.bind(this);
        this.overlay = document.getElementById(domUtils_1.MABL_OVERLAY_ID);
        this.requestAttention = requestAttention;
        this.overlayClassname = overlayClassname;
        this.hideInIframe = hideInIframe;
        this.addHighlightBox = addHighlightBox;
    }
    checkAndApplyIframeHandling(event) {
        if (this.overlay?.style) {
            if (this.hasFrameInCoordinates(document, event.clientX, event.clientY)) {
                this.overlay.style.pointerEvents = 'none';
            }
            else {
                this.overlay.style.pointerEvents = 'auto';
            }
        }
    }
    hasFrameInCoordinates(document, clientX, clientY, collectedElements = new Set()) {
        const targets = document
            .elementsFromPoint(clientX, clientY)
            .filter((element) => element.id !== domUtils_1.MABL_OVERLAY_ID && element.id !== domUtils_1.MABL_HIGHLIGHT_ID);
        if (targets.length === 0) {
            return false;
        }
        for (const target of targets) {
            if (target.tagName === 'IFRAME') {
                return true;
            }
            if (target.shadowRoot && !collectedElements.has(target)) {
                collectedElements.add(target);
                const hasFrames = this.hasFrameInCoordinates(target.shadowRoot, clientX, clientY, collectedElements);
                if (hasFrames) {
                    return true;
                }
            }
        }
        return false;
    }
    shouldIgnoreClicks() {
        return this.overlay?.style.pointerEvents === 'none';
    }
    register() {
        (0, domUtils_1.destroySelectionOverlay)();
        if ((0, domUtils_1.isThisAnIframe)() && !this.hideInIframe) {
            (0, domUtils_1.renderSelectionOverlay)({ innerTextDisplayed: '' });
        }
        else {
            const isInWalkthrough = this.store.getState().walkthrough.isInProgress;
            (0, domUtils_1.renderSelectionOverlay)({
                innerTextDisplayed: this.overlayText,
                requestAttention: this.requestAttention,
                isInWalkthrough,
                subtext: this.overlaySubtext,
                overlayClassname: this.overlayClassname,
                addHighlightBox: this.addHighlightBox,
            });
        }
        this.overlay = document.getElementById(domUtils_1.MABL_OVERLAY_ID);
        window.addEventListener('scroll', this.handleScroll, { capture: true });
    }
    unregister() {
        window.removeEventListener('scroll', this.handleScroll, { capture: true });
        (0, domUtils_1.destroySelectionOverlay)();
    }
    handleScroll(_event) { }
}
exports.default = OverlayHandler;
