"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenAppExecutor = void 0;
exports.openAppAction = openAppAction;
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
class OpenAppExecutor {
    run(step, executionContext) {
        return (0, execution_1.runActionWithElementReadyRetriesForMobile)(() => this.openApp(step, executionContext), executionContext);
    }
    async openApp(step, executionContext) {
        const driver = executionContext.mobileTestEnvironment.getDriver();
        const platform = executionContext.mobileTestEnvironment.getConfig().app.platformName;
        const appId = step.packageName ??
            (await executionContext.mobileTestEnvironment.getAppUnderTestId());
        try {
            await openAppAction(appId, driver, platform);
            return {
                status: execution_2.StepStatus.Passed,
            };
        }
        catch (error) {
            executionContext.log.error(error);
            return Promise.resolve({
                status: execution_2.StepStatus.Failed,
                logInfo: `Error opening the app`,
            });
        }
    }
}
exports.OpenAppExecutor = OpenAppExecutor;
function openAppAction(appId, driver, platform) {
    switch (platform) {
        case mablApi_1.MobilePlatformEnum.Android:
            return driver.execute('mobile: activateApp', {
                appId,
            });
        case mablApi_1.MobilePlatformEnum.Ios:
            return driver.execute('mobile: launchApp', {
                bundleId: appId,
            });
    }
}
