"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEventMessageWithShadowParents = createEventMessageWithShadowParents;
exports.createBaseEventMessageWithShadowParents = createBaseEventMessageWithShadowParents;
exports.getVisibleScrollableElements = getVisibleScrollableElements;
exports.extractSelectOptions = extractSelectOptions;
const util_1 = require("./eventHandlers/util");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const shadowDomUtils_1 = require("../sharedUtils/shadowDomUtils");
async function createEventMessageWithShadowParents(target, action, listener, xPath, sourceEventTimeStamp, shadowParents) {
    if (!shadowParents) {
        shadowParents = (0, shadowDomUtils_1.maybeGetShadowParentsFromElement)(target);
    }
    const eventMessage = await (0, util_1.createEventMessageWithAncestor)(target, action, listener, xPath, sourceEventTimeStamp);
    return (0, shadowDomUtils_1.addShadowParentInfoToMessage)(target, eventMessage, shadowParents);
}
function createBaseEventMessageWithShadowParents(event, action, listener, xPath, sourceEventTimeStamp, context) {
    const shadowParents = (0, shadowDomUtils_1.maybeGetShadowParents)(event);
    const finalTarget = shadowParents
        ? (0, util_1.getPathOnEvent)(event)[0]
        : event.target;
    return (0, shadowDomUtils_1.createBaseEventMessageWithShadowParentsFromElement)(finalTarget, action, listener, xPath, sourceEventTimeStamp, (0, util_1.getCustomTestIdAttributes)(), context, shadowParents);
}
function getVisibleScrollableElements() {
    const scrollableElements = [];
    const elements = document.body.getElementsByTagName('*');
    for (const element of elements) {
        const scrollability = (0, mablscriptFind_1.getScrollability)(element);
        if (scrollability) {
            const boundingBox = element.getBoundingClientRect();
            scrollableElements.push({
                type: 'element',
                element,
                elementBoundingBox: {
                    y: boundingBox.y,
                    x: boundingBox.x,
                    width: boundingBox.width,
                    height: boundingBox.height,
                },
                scrollability,
                isPartiallyVisible: (0, mablscriptFind_1.isPartiallyInViewport)(boundingBox),
            });
        }
    }
    const sortedScrollableElements = scrollableElements.sort((elementA, elementB) => (elementA.elementBoundingBox?.height ?? 0) *
        (elementA.elementBoundingBox?.width ?? 0) -
        (elementB.elementBoundingBox?.height ?? 0) *
            (elementB.elementBoundingBox?.width ?? 0));
    return sortedScrollableElements;
}
function extractSelectOptions(element) {
    if (element.tagName.toLowerCase() !== 'select') {
        return undefined;
    }
    const selectElement = element;
    const options = [];
    for (const option of selectElement.options) {
        options.push({
            value: option.value || option.text,
            text: option.text,
        });
    }
    return options.length > 0 ? options : undefined;
}
