"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OjSelectOne = void 0;
const OjSelectOneBase_1 = require("./OjSelectOneBase");
const oj_module_proxy_1 = require("../../lib/oj-module-proxy");
/**
 * The component WebElement for [oj-select-one](../../jsdocs/oj.ojSelectOne.html).
 * Do not instantiate this class directly, instead, use
 * [ojSelectOne](../functions/elements.ojSelectOne-1.html).
 */
class OjSelectOne extends OjSelectOneBase_1.OjSelectOneBase {
    /**
     * Sets the value of "value" property.
     * The value of the element.
     * @param value The value to set for "value"
     * @override
     */
    async changeValue(value) {
        // Call focus() on the root element
        await this.getDriver().executeScript('arguments[0].focus()', this);
        // Only mutate if not readonly/disabled
        const readonly = await this.getReadonly();
        const disabled = await this.getDisabled();
        if (!(readonly || disabled)) {
            await this.whenBusyContextReady();
            const isValueSet = await (0, oj_module_proxy_1.executeWithModules)(this.getDriver(), ['CustomElementUtils'], [this, value], `
        (({ getElementBridge }, element, value) => {
          // Retrieve the widget instance from the element
          var bridge = getElementBridge(element);
          var widget = bridge._WIDGET_INSTANCE.select;
          return widget._selectItemByValue(value)
            .then(() => true)
            .catch(() => false);
        })
        `);
            // If failed to set value, fallback to setProperty
            if (isValueSet === false) {
                console.warn('Failed to update value by interaction. Falling back to setProperty.');
                await this._changeValueFallback(value);
            }
        }
    }
    /**
     * Clears the value
     * @override
     */
    clear() {
        return this.setProperty('value', null);
    }
    /**
     * This is a fallback mechanism for setting a value which
     * uses the setProperty method to update the value.
     *
     * @param value The value to be set
     */
    _changeValueFallback(value) {
        return this.setProperty('value', value);
    }
}
exports.OjSelectOne = OjSelectOne;
//# sourceMappingURL=OjSelectOne.js.map