"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeGetShadowRootHost = maybeGetShadowRootHost;
exports.maybeGetParentShadowRoot = maybeGetParentShadowRoot;
exports.checkParentForShadowRoot = checkParentForShadowRoot;
exports.maybeGetShadowParents = maybeGetShadowParents;
exports.maybeGetShadowParentsFromElement = maybeGetShadowParentsFromElement;
exports.createBaseEventMessageWithShadowParentsFromElement = createBaseEventMessageWithShadowParentsFromElement;
exports.addShadowParentInfoToMessage = addShadowParentInfoToMessage;
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
const browser_1 = require("@mablhq-internal/mablscript-find/browser");
const eventMessages_1 = require("./eventMessages");
function getTargetElementFromEventPath(event) {
    return (event.composedPath()?.[0] ?? event.target);
}
function maybeGetShadowRootHost(event) {
    const targetElement = getTargetElementFromEventPath(event);
    return (0, mablscriptFind_1.maybeGetShadowRootHostFromElement)(targetElement);
}
function maybeGetParentShadowRoot(event) {
    const targetElement = getTargetElementFromEventPath(event);
    return checkParentForShadowRoot(targetElement);
}
function checkParentForShadowRoot(targetElement) {
    if (targetElement?.shadowRoot) {
        return targetElement;
    }
    else if (targetElement.parentElement) {
        return checkParentForShadowRoot(targetElement.parentElement);
    }
    return undefined;
}
function maybeGetShadowParents(event) {
    const shadowRootHostElement = maybeGetShadowRootHost(event);
    if (shadowRootHostElement) {
        return (0, browser_1.evaluateShadowParentFindability)(shadowRootHostElement);
    }
    return [];
}
function maybeGetShadowParentsFromElement(element) {
    return (0, browser_1.evaluateShadowParentFindability)(element);
}
function createBaseEventMessageWithShadowParentsFromElement(element, action, listener, xPath, sourceEventTimeStamp, customTestIdAttributes, context, shadowParents) {
    if (!shadowParents) {
        shadowParents = maybeGetShadowParentsFromElement(element);
    }
    const initialMessage = (0, eventMessages_1.createBaseContentEventMessage)(element, action, listener, xPath, sourceEventTimeStamp, customTestIdAttributes, context);
    return addShadowParentInfoToMessage(element, initialMessage, shadowParents);
}
function addShadowParentInfoToMessage(target, eventMessage, shadowParents) {
    if (shadowParents?.length) {
        eventMessage.shadowParentsFindingConfidence = mablscriptFind_1.ConfidenceLevel.HIGH;
        eventMessage.shadowParents = [];
        for (const shadow of shadowParents) {
            const shadowSelector = (0, mablscriptFind_1.elementToSelector)(shadow.target);
            shadowSelector.xpath = shadow.xpath;
            eventMessage.shadowParents.push(shadowSelector);
            eventMessage.shadowParentsFindingConfidence =
                shadow.confidenceLevel === mablscriptFind_1.ConfidenceLevel.LOW
                    ? mablscriptFind_1.ConfidenceLevel.LOW
                    : eventMessage.shadowParentsFindingConfidence;
        }
        const targetElPath = (0, mablscriptFind_1.getPathForElement)(target);
        const newTargetXpath = (0, mablscriptFind_1.createXpathFromPathArrayForShadowDom)(targetElPath);
        if (eventMessage.elementSelectors) {
            if (Array.isArray(eventMessage.elementSelectors)) {
                eventMessage.elementSelectors.forEach((elementSelector) => (elementSelector.xpath = newTargetXpath));
            }
            else {
                eventMessage.elementSelectors.xpath = newTargetXpath;
            }
        }
    }
    return eventMessage;
}
