"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstallAppExecutor = void 0;
exports.installAppAction = installAppAction;
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
const INSTALL_TIMEOUT_MILLIS = 120_000;
class InstallAppExecutor {
    run(step, executionContext) {
        return (0, execution_1.runActionWithElementReadyRetriesForMobile)(() => this.install(step, executionContext), executionContext);
    }
    async install(_step, executionContext) {
        const appPath = executionContext.mobileTestEnvironment.getConfig().app.path;
        if (!appPath) {
            return Promise.resolve({
                status: execution_2.StepStatus.Failed,
                logInfo: 'No app path supplied for installing the application',
            });
        }
        const driver = executionContext.mobileTestEnvironment.getDriver();
        const platform = executionContext.mobileTestEnvironment.getConfig().app.platformName;
        const appId = await executionContext.mobileTestEnvironment.getAppUnderTestId();
        try {
            await installAppAction(appPath, 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 installing the app`,
            });
        }
    }
}
exports.InstallAppExecutor = InstallAppExecutor;
async function installAppAction(appPath, appId, driver, platform) {
    switch (platform) {
        case mablApi_1.MobilePlatformEnum.Android:
            await driver.execute('mobile: installApp', {
                appPath,
                timeout: INSTALL_TIMEOUT_MILLIS,
            });
            if (appId) {
                await driver.execute('mobile: activateApp', {
                    appId,
                });
            }
            break;
        case mablApi_1.MobilePlatformEnum.Ios:
            await driver.execute('mobile: installApp', {
                app: appPath,
                timeoutMs: INSTALL_TIMEOUT_MILLIS,
                strategy: 'serial',
            });
            if (appId) {
                await driver.execute('mobile: launchApp', {
                    bundleId: appId,
                });
            }
    }
}
