"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ELEMENT_COMMANDS = exports.UIElement = void 0;
exports.elementFromResponse = elementFromResponse;
const W3C_ELEMENT_KEY = 'element-6066-11e4-a720-4889042c0703';
const LEGACY_W3C_ELEMENT_KEY = 'element-6066-11e4-a52e-4f735466cecf';
const JWP_ELEMENT_KEY = 'ELEMENT';
const ELEMENT_KEYS = [W3C_ELEMENT_KEY, LEGACY_W3C_ELEMENT_KEY, JWP_ELEMENT_KEY];
const ELEMENT_COMMANDS = {
    isElementSelected: 'isSelected',
    isElementDisplayed: 'isDisplayed',
    getElementAttribute: 'getAttribute',
    getElementCSSValue: 'getCSSValue',
    getElementText: 'getText',
    getElementTagName: 'getTagName',
    getElementLocation: 'getLocation',
    getElementLocationInView: 'getLocationInView',
    getElementProperty: 'getProperty',
    getElementRect: 'getRect',
    getElementSize: 'getSize',
    getElementEnabled: 'getEnabled',
    elementClick: 'click',
    elementSubmit: 'submit',
    elementClear: 'clear',
    elementSendKeys: 'sendKeys',
    takeElementScreenshot: 'takeScreenshot',
};
exports.ELEMENT_COMMANDS = ELEMENT_COMMANDS;
class UIElement {
    elementKey;
    elementId;
    session;
    constructor(elementKey, findResponse, parent) {
        this.elementKey = elementKey;
        this.elementId = findResponse[elementKey];
        this[elementKey] = this.elementId;
        this.session = parent instanceof UIElement ? parent.session : parent;
    }
    get executeObj() {
        return { [this.elementKey]: this.elementId };
    }
    async findElement(using, value) {
        const response = await this.session.cmd('findElementFromElement', this.elementId, using, value);
        return elementFromResponse(response, this);
    }
    async findElements(using, value) {
        const responses = (await this.session.cmd('findElementsFromElement', this.elementId, using, value));
        return responses.map((response) => elementFromResponse(response, this));
    }
}
exports.UIElement = UIElement;
for (const [wireCommand, methodName] of Object.entries(ELEMENT_COMMANDS)) {
    UIElement.prototype[methodName] = async function (...args) {
        return this.session.cmd(wireCommand, this.elementId, ...args);
    };
}
function elementFromResponse(response, parent) {
    const elementResponse = (response ?? {});
    const elementKey = ELEMENT_KEYS.find((key) => elementResponse[key] !== undefined);
    if (!elementKey) {
        throw new Error(`Bad findElement response; did not have element key. Response was: ${JSON.stringify(response)}`);
    }
    return new UIElement(elementKey, elementResponse, parent);
}
