"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UninstallAppExecutor = void 0;
exports.dismissAlerts = dismissAlerts;
exports.uninstallAppAction = uninstallAppAction;
const execution_1 = require("@mablhq-internal/runtime/execution");
const execution_2 = require("@mablhq-internal/runtime/execution");
const mobileUtil_1 = require("../util/mobileUtil");
const androidLocators_1 = require("../actions/find/android/androidLocators");
const types_1 = require("../types");
const AndroidCandidate_1 = __importDefault(require("../actions/find/android/AndroidCandidate"));
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
class UninstallAppExecutor {
    run(step, executionContext) {
        return (0, execution_1.runActionWithElementReadyRetriesForMobile)(() => this.uninstall(step, executionContext), executionContext);
    }
    async uninstall(step, executionContext) {
        const driver = executionContext.mobileTestEnvironment.getDriver();
        const platform = executionContext.mobileTestEnvironment.getConfig().app.platformName;
        const getAppUnderTestId = () => executionContext.mobileTestEnvironment.getAppUnderTestId();
        try {
            await dismissAlerts(driver, platform, getAppUnderTestId);
        }
        catch (error) {
            executionContext.log.warning(`Error dismissing alerts: ${error}`);
        }
        const appId = step.appId ?? (await getAppUnderTestId());
        if (!appId) {
            return Promise.resolve({
                status: execution_2.StepStatus.Failed,
                logInfo: 'No app package ID found for uninstalling the application',
            });
        }
        try {
            await uninstallAppAction(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 uninstalling the app`,
            });
        }
    }
}
exports.UninstallAppExecutor = UninstallAppExecutor;
async function dismissAlerts(driver, platform, getAppUnderTestId) {
    let remainingAttempts = 3;
    switch (platform) {
        case mablApi_1.MobilePlatformEnum.Android:
            while ((0, mobileUtil_1.isSystemApp)(await getAppUnderTestId()) && remainingAttempts > 0) {
                --remainingAttempts;
                const buttons = await androidLocators_1.locateByClass.locate(driver, {
                    class: types_1.AndroidElementType.button,
                });
                const buttonCandidates = [...buttons].map((button) => AndroidCandidate_1.default.fromElementReference(button));
                const systemAlertButtonCandidate = buttonCandidates.find((button) => (0, mobileUtil_1.isSystemApp)(button.selector.package));
                if (systemAlertButtonCandidate?.elementReference) {
                    const systemAlertButtonElement = await driver
                        .$(systemAlertButtonCandidate.elementReference)
                        .getElement();
                    await systemAlertButtonElement.click();
                }
            }
            break;
        case mablApi_1.MobilePlatformEnum.Ios:
            while ((0, mobileUtil_1.isSystemApp)(await getAppUnderTestId()) && remainingAttempts > 0) {
                --remainingAttempts;
                await driver.execute('mobile: alert', { action: 'dismiss' });
            }
    }
}
async function uninstallAppAction(appId, driver, platform) {
    switch (platform) {
        case mablApi_1.MobilePlatformEnum.Android:
            await driver.execute('mobile: removeApp', {
                appId,
            });
            break;
        case mablApi_1.MobilePlatformEnum.Ios:
            await driver.execute('mobile: removeApp', {
                bundleId: appId,
            });
    }
}
