"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dom_util_1 = require("@mablhq-internal/mablscript-find/dom-util");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const browser_1 = require("@mablhq-internal/mablscript-find/browser");
if (window.__moblInit) {
    throw new Error('Script already loaded and initialized');
}
if (document.readyState === 'complete') {
    init();
}
else {
    window.addEventListener('DOMContentLoaded', init, true);
}
function init() {
    addOverlay();
    window.__moblInit = true;
    window.__mobl = {
        lastWebViewDescriptor: undefined,
        getLastWebViewDescriptor,
        reset,
        hideOverlay,
    };
}
let overlayElement;
const findLogger = new mablscriptFind_1.Logger();
function addOverlay() {
    const MABL_OVERLAY_Z_INDEX = 2_147_483_647;
    overlayElement = (0, dom_util_1.createIgnoredDomElement)('div');
    overlayElement.id = dom_util_1.MABL_OVERLAY_ID;
    overlayElement.style.cssText =
        [
            'width: 100%',
            'height: 100%',
            'top:0',
            'left:0',
            'position: fixed',
        ].join(';') + ';';
    overlayElement.addEventListener('click', overlayClickHandler);
    window.document.body.appendChild(overlayElement);
    overlayElement.style.zIndex = MABL_OVERLAY_Z_INDEX.toString();
}
function overlayClickHandler(event) {
    hideOverlay();
    const element = window.document.elementFromPoint(event.clientX, event.clientY);
    const selectorSearchResult = (0, browser_1.createFindOneSelector)(element, findLogger);
    let findSpecification;
    const maybeSelector = selectorSearchResult.chosenElementSelectorCandidate?.selector;
    if (!maybeSelector) {
        const selector = (0, mablscriptFind_1.elementToSelector)(element);
        findSpecification = { selector };
    }
    else if ((0, mablscriptFind_1.isStandardSelector)(maybeSelector)) {
        findSpecification = {
            selector: maybeSelector,
            type: mablscriptFind_1.FindType.FIND_ONE,
        };
    }
    else {
        const [selector, ancestorSelector] = maybeSelector;
        findSpecification = {
            auxiliaryDescriptors: [{ selector: ancestorSelector }],
            selector,
        };
    }
    window.__mobl.lastWebViewDescriptor = {
        webViewDescriptor: {
            webView: {
                descriptor: {
                    findType: mablscriptFind_1.FindType.FIND_ONE,
                    findTarget: findSpecification,
                },
                title: window.document.title,
                url: window.location.href,
            },
        },
        window: {
            visualViewportScale: window.visualViewport?.scale ?? 1,
            visualViewportWidth: window.visualViewport?.width ?? window.outerWidth,
            visualViewportOffsetLeft: window.visualViewport?.offsetLeft ?? 0,
            visualViewportOffsetTop: window.visualViewport?.offsetTop ?? 0,
        },
        elementBoundingClientRect: element.getBoundingClientRect().toJSON(),
    };
}
function getLastWebViewDescriptor() {
    if (!window.__mobl.lastWebViewDescriptor) {
        return undefined;
    }
    const lastFindDescriptor = window.__mobl.lastWebViewDescriptor;
    window.__mobl.lastWebViewDescriptor = undefined;
    return lastFindDescriptor;
}
function reset() {
    window.__mobl.lastWebViewDescriptor = undefined;
    overlayElement.style.display = '';
}
function hideOverlay() {
    overlayElement.style.display = 'none';
}
