"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stubs = void 0;
function stubs() {
    let hostEl = null;
    installStubs();
    function installStubs() {
        const oldAddEventListener = Window.prototype.addEventListener;
        Window.prototype.addEventListener = function addEventListener(event) {
            if (event === 'beforeunload' || event === 'unload') {
                logMessage('Dropped beforeunload (via window.addEventListener)');
                return;
            }
            oldAddEventListener.apply(this, arguments);
        };
        const oldAddEventListenerBody = HTMLBodyElement.prototype.addEventListener;
        HTMLBodyElement.prototype.addEventListener = function addEventListener(event) {
            if (event === 'beforeunload' || event === 'unload') {
                logMessage('Dropped beforeonload (via body.addEventListener)');
                return;
            }
            oldAddEventListenerBody.apply(this, arguments);
        };
        Object.defineProperty(HTMLBodyElement.prototype, 'onbeforeunload', {
            get() {
                return null;
            },
            set() {
                logMessage('Dropped beforeunload (via body.onbeforeunload)');
            },
        });
        removeUnloadEvents(window);
        document.addEventListener('DOMContentLoaded', () => {
            removeUnloadEvents(document.body);
        });
        Object.assign(window, {
            __alert: window.alert,
            __confirm: window.confirm,
            __prompt: window.prompt,
        });
        window.alert = alertStub;
        window.confirm = confirmStub;
        window.prompt = promptStub;
    }
    function alertStub(message) {
        const root = getHostEl();
        const existing = root.querySelectorAll('[data-r1-alert]');
        for (const el of existing) {
            el.parentNode.removeChild(el);
        }
        const el = document.createElement('div');
        root.appendChild(el);
        el.setAttribute('data-r1', '');
        el.setAttribute('data-r1-alert', '');
        el.setAttribute('style', `
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 16px;
            background: gold;
        `);
        el.innerText = message;
        el.addEventListener('dblclick', () => el.parentNode.removeChild(el));
    }
    function confirmStub() {
        logMessage('Suppressed confirm, returning true');
        return true;
    }
    function promptStub(title, value) {
        logMessage('Suppressed prompt, returning default value');
        return value || null;
    }
    function logMessage(message) {
        console.log('✈︎✈︎✈︎ ' + message);
    }
    function removeUnloadEvents(el) {
        el.onbeforeunload = null;
        el.onunload = null;
        Object.defineProperty(el, 'onbeforeunload', {
            get() {
                return null;
            },
            set() {
                logMessage(`Dropped beforeunload (via <${el.tagName} onbeforeunload="">)`);
            },
        });
        try {
            Object.defineProperty(el, 'onunload', {
                get() {
                    return null;
                },
                set() {
                    logMessage(`Dropped beforeunload (via <${el.tagName} onunload="">)`);
                },
            });
        }
        catch (e) { }
        try {
            el.setAttribute('onbeforeunload', '');
            el.setAttribute('onunload', '');
        }
        catch (e) { }
    }
    function getHostEl() {
        if (!hostEl) {
            hostEl = document.createElement('div');
            document.documentElement.appendChild(hostEl);
            hostEl.style.zIndex = '9999999';
            hostEl.style.position = 'fixed';
            hostEl.style.top = '0';
            hostEl.style.left = '0';
        }
        return hostEl;
    }
}
exports.stubs = stubs;
//# sourceMappingURL=stubs.js.map