"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HideKeyboardExecutor = void 0;
exports.androidHideKeyboard = androidHideKeyboard;
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
class HideKeyboardExecutor {
    run(step, executionContext) {
        return (0, execution_1.runActionWithElementReadyRetriesForMobile)(() => this.hideKeyboard(step, executionContext), executionContext);
    }
    async hideKeyboard(_step, executionContext) {
        const driver = executionContext.mobileTestEnvironment.getDriver();
        const platform = executionContext.mobileTestEnvironment.getConfig().app.platformName;
        try {
            await executionContext.observeDeferredFunction();
            switch (platform) {
                case mablApi_1.MobilePlatformEnum.Android:
                    await androidHideKeyboard(driver);
                    break;
                case mablApi_1.MobilePlatformEnum.Ios:
                    throw new Error('Hiding the keyboard is not supported on iOS.');
            }
        }
        catch (error) {
            executionContext.log.error(error);
            return {
                status: execution_2.StepStatus.Failed,
                logInfo: 'Unable to hide the keyboard',
            };
        }
        return { status: execution_2.StepStatus.Passed };
    }
}
exports.HideKeyboardExecutor = HideKeyboardExecutor;
async function androidHideKeyboard(driver) {
    try {
        await driver.execute('mobile: hideKeyboard');
    }
    catch (error) {
        loggingProvider_1.logger.warn(`Attempted to hide the keyboard but the call threw — assuming there was no keyboard to dismiss and continuing. Underlying error: ${error?.message ?? String(error)}`);
    }
}
