"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MablStepWithFindAction = void 0;
exports.isStepWithFindAction = isStepWithFindAction;
exports.humanizeFindDescriptor = humanizeFindDescriptor;
exports.humanizeCustomFind = humanizeCustomFind;
exports.humanizeQuery = humanizeQuery;
exports.humanizeFindEmail = humanizeFindEmail;
exports.humanizeFindCookie = humanizeFindCookie;
exports.humanizeFindTab = humanizeFindTab;
exports.getTagNameFromFoundSelectors = getTagNameFromFoundSelectors;
exports.isCustomFindDescriptor = isCustomFindDescriptor;
exports.isEmailFindDescriptor = isEmailFindDescriptor;
exports.isCookieFindDescriptor = isCookieFindDescriptor;
exports.isTabFindDescriptor = isTabFindDescriptor;
const mablscriptFind_1 = require("./mablscriptFind");
const FindAction_1 = require("./actions/FindAction");
const MablStepV2_1 = require("./MablStepV2");
const stepUtil_1 = require("./mobile/steps/stepUtil");
class MablStepWithFindAction extends MablStepV2_1.MablStepV2 {
    findAction;
    constructor(name, descriptor, actionCode, findAction) {
        super(name, descriptor, actionCode);
        this.findAction =
            findAction ?? FindAction_1.FindAction.findActionFromStepArgs(descriptor);
        this.actions = [this.findAction];
    }
    humanizeFind() {
        const findDescriptor = this.findAction.toDescriptor();
        return humanizeFindDescriptor(findDescriptor);
    }
}
exports.MablStepWithFindAction = MablStepWithFindAction;
function isStepWithFindAction(step) {
    return step instanceof MablStepWithFindAction || 'findAction' in step;
}
function humanizeFindDescriptor(findDescriptor) {
    switch (findDescriptor.findType) {
        case mablscriptFind_1.FindType.FIND_FIRST:
        case mablscriptFind_1.FindType.FIND_LAST:
        case mablscriptFind_1.FindType.FIND_ALL:
            return humanizeCustomFind(findDescriptor);
        case mablscriptFind_1.FindType.FIND_ONE:
            const maybeMobileFindDescriptor = (0, mablscriptFind_1.isMobileFindSpecification)(findDescriptor.findTarget)
                ? {
                    findTarget: { ...findDescriptor.findTarget },
                    findType: findDescriptor.findType,
                }
                : findDescriptor;
            if ((0, mablscriptFind_1.isMobileFindDescriptor)(maybeMobileFindDescriptor)) {
                return (0, stepUtil_1.getElementDescriptionFromMobileFindDescriptor)(maybeMobileFindDescriptor);
            }
            return (0, mablscriptFind_1.humanizeFindOneDescriptor)(findDescriptor.findTarget);
        case mablscriptFind_1.FindType.FIND_EMAIL:
            return humanizeFindEmail(findDescriptor);
        case mablscriptFind_1.FindType.FIND_COOKIE:
            return humanizeFindCookie(findDescriptor);
        case mablscriptFind_1.FindType.FIND_TAB:
            return humanizeFindTab(findDescriptor);
        default:
            return 'element';
    }
}
function humanizeCustomFind(findDescriptor) {
    if (!isCustomFindDescriptor(findDescriptor)) {
        return 'element';
    }
    const type = findDescriptor.findType.replace('find_', '');
    let tagText;
    const foundElementSelectors = findDescriptor.foundElementSelectors;
    const tagName = getTagNameFromFoundSelectors(foundElementSelectors);
    if (tagName) {
        tagText = `${type} ${tagName.toLowerCase()} element`;
    }
    else {
        const s = findDescriptor.findType === mablscriptFind_1.FindType.FIND_ALL ? 's' : '';
        tagText = `${type} element${s}`;
    }
    return `${tagText} ${humanizeQuery(findDescriptor)}`;
}
function humanizeQuery(findDescriptor) {
    const primarySelectors = findDescriptor.primarySelectors;
    if ((0, mablscriptFind_1.isCssSelector)(primarySelectors)) {
        const compactQuery = primarySelectors.css_query.replace(/\s+/g, ' ').trim();
        return `matching css query "${compactQuery}"`;
    }
    else if ((0, mablscriptFind_1.isXPathSelector)(primarySelectors)) {
        const compactQuery = primarySelectors.xpath.replace(/\s+/g, ' ').trim();
        return `matching xpath "${compactQuery}"`;
    }
    return 'matching "<unrecognized query>"';
}
function humanizeFindEmail(findDescriptor) {
    if (!isEmailFindDescriptor(findDescriptor)) {
        return 'email';
    }
    return (0, mablscriptFind_1.humanizeEmailSelector)(findDescriptor.findTarget.selector);
}
function humanizeFindCookie(findDescriptor) {
    if (!isCookieFindDescriptor(findDescriptor)) {
        return 'cookie';
    }
    return (0, mablscriptFind_1.humanizeCookieSelector)(findDescriptor.findTarget);
}
function humanizeFindTab(findDescriptor) {
    if (!isTabFindDescriptor(findDescriptor)) {
        return 'tab';
    }
    if (findDescriptor.findTarget === 'initial') {
        return 'the initial tab';
    }
    return (0, mablscriptFind_1.humanizeTabSelector)(findDescriptor.findTarget);
}
function getTagNameFromFoundSelectors(foundElementSelectors) {
    if (!foundElementSelectors) {
        return undefined;
    }
    if (typeof foundElementSelectors === 'object' &&
        'tag_name' in foundElementSelectors) {
        return foundElementSelectors.tag_name;
    }
    return undefined;
}
function isCustomFindDescriptor(descriptor) {
    return ((descriptor.findType === mablscriptFind_1.FindType.FIND_FIRST ||
        descriptor.findType === mablscriptFind_1.FindType.FIND_LAST ||
        descriptor.findType === mablscriptFind_1.FindType.FIND_ALL) &&
        'primarySelectors' in descriptor);
}
function isEmailFindDescriptor(descriptor) {
    return descriptor.findType === mablscriptFind_1.FindType.FIND_EMAIL;
}
function isCookieFindDescriptor(descriptor) {
    return descriptor.findType === mablscriptFind_1.FindType.FIND_COOKIE;
}
function isTabFindDescriptor(descriptor) {
    return descriptor.findType === mablscriptFind_1.FindType.FIND_TAB;
}
