"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSimpleAndroidElementName = getSimpleAndroidElementName;
exports.getSimpleIOSElementName = getSimpleIOSElementName;
exports.getDescriptionForMobileStepDescriptor = getDescriptionForMobileStepDescriptor;
exports.getElementDescriptionFromMobileFindDescriptor = getElementDescriptionFromMobileFindDescriptor;
exports.humanizeCustomFind = humanizeCustomFind;
exports.isStepWithFind = isStepWithFind;
exports.isPasswordField = isPasswordField;
const mablscriptFind_1 = require("../../mablscriptFind");
function getSimpleAndroidElementName(className) {
    if (!className) {
        return 'unknown';
    }
    const parts = className.split('.');
    return parts[parts.length - 1];
}
function getSimpleIOSElementName(elementName) {
    if (!elementName) {
        return 'unknown';
    }
    const parts = elementName.split('XCUIElementType');
    if (parts.length > 1) {
        return parts[1];
    }
    return 'unknown';
}
function getDescriptionForMobileStepDescriptor(predicateAction, descriptor) {
    const elementDescription = getElementDescriptionFromMobileFindDescriptor(descriptor.find);
    return `${predicateAction} ${elementDescription}`;
}
function getElementDescriptionFromMobileFindDescriptor(descriptor) {
    const findTarget = descriptor?.findTarget;
    if ((0, mablscriptFind_1.isMobileFindSpecification)(findTarget)) {
        if ((0, mablscriptFind_1.isWebViewFindElementDescriptor)(descriptor)) {
            return getDescriptionForWebViewFindDescriptor(descriptor);
        }
        const selector = findTarget.selector;
        if (selector.android && Object.keys(selector.android).length) {
            return buildElementDescriptionForAndroid(findTarget);
        }
        else if (selector.iOS) {
            return buildElementDescriptionForiOS(findTarget);
        }
    }
    return 'unknown element';
}
function buildElementDescriptionForAndroid(findTarget) {
    const selector = findTarget.selector;
    const androidElementSelector = selector.android;
    if (!androidElementSelector) {
        return `unknown android element`;
    }
    if (androidElementSelector.find_prompt) {
        return `${(0, mablscriptFind_1.normalizeText)(androidElementSelector.find_prompt)}`;
    }
    const description = `"${getSimpleAndroidElementName(androidElementSelector.class)}" element`;
    if (androidElementSelector?.hint) {
        return `${description} with hint "${(0, mablscriptFind_1.normalizeText)(androidElementSelector.hint)}"`;
    }
    if (androidElementSelector?.text) {
        return `${description} with text "${(0, mablscriptFind_1.normalizeText)(androidElementSelector.text)}"`;
    }
    if (androidElementSelector?.contentDesc) {
        return `${description} with content-desc "${(0, mablscriptFind_1.normalizeText)(androidElementSelector.contentDesc)}"`;
    }
    return description;
}
function buildElementDescriptionForiOS(findTarget) {
    const selector = findTarget.selector;
    const iOSElementSelector = selector.iOS;
    if (!iOSElementSelector) {
        return `unknown iOS element`;
    }
    if (iOSElementSelector.find_prompt) {
        return `${(0, mablscriptFind_1.normalizeText)(iOSElementSelector.find_prompt)}`;
    }
    if (iOSElementSelector.name) {
        return `"${iOSElementSelector.name}" element`;
    }
    const simpleDescription = iOSElementSelector.type
        ? `"${getSimpleIOSElementName(iOSElementSelector.type)}" element`
        : `element`;
    if (iOSElementSelector.label) {
        return `${simpleDescription} with label "${(0, mablscriptFind_1.normalizeText)(iOSElementSelector.label)}"`;
    }
    return simpleDescription;
}
function getDescriptionForWebViewFindDescriptor(webViewElementDescriptor) {
    if ((0, mablscriptFind_1.isFindElementDescriptor)(webViewElementDescriptor.webView.descriptor)) {
        return (0, mablscriptFind_1.humanizeFindOneDescriptor)(webViewElementDescriptor.webView.descriptor.findTarget);
    }
    return `unknown webview element`;
}
function humanizeCustomFind(descriptor) {
    if (isStepWithFind(descriptor)) {
        if ((0, mablscriptFind_1.isMobileFindCustomDescriptor)(descriptor.find)) {
            return `xpath: ${descriptor.find.findTarget.xpath}`;
        }
    }
    return `custom find`;
}
function isStepWithFind(value) {
    return !!value?.find;
}
function isPasswordField(descriptor) {
    if ((0, mablscriptFind_1.isMobileFindSpecification)(descriptor?.findTarget)) {
        if (descriptor.findTarget.selector.android?.password === true) {
            return true;
        }
        if (descriptor.findTarget.selector.iOS?.type ===
            'XCUIElementTypeSecureTextField') {
            return true;
        }
    }
    return false;
}
