"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenLinkExecutor = void 0;
exports.openDeepLink = openDeepLink;
exports.waitForPackageToBeActive = waitForPackageToBeActive;
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const execution_1 = require("@mablhq-internal/runtime/execution");
const async_retry_1 = __importDefault(require("async-retry"));
class OpenLinkExecutor {
    run(step, executionContext) {
        return this.openLink(step, executionContext);
    }
    async openLink(step, executionContext) {
        const platform = executionContext.mobileTestEnvironment.getConfig().app.platformName;
        const driver = executionContext.mobileTestEnvironment.getDriver();
        try {
            const finalUrl = `${executionContext.variables.resolve(step.url)}`;
            const finalPackageName = `${executionContext.variables.resolve(step.packageName)}`;
            await openDeepLink(finalUrl, finalPackageName, driver, platform, executionContext.mobileTestEnvironment.getConfig().app.platformVersion);
            try {
                await waitForPackageToBeActive(step.packageName, driver, platform);
            }
            catch (error) {
                executionContext.log.warning(`Unable to determine if expected app opened`, error);
            }
            return {
                status: execution_1.StepStatus.Passed,
            };
        }
        catch (error) {
            executionContext.log.error(error);
            return Promise.resolve({
                status: execution_1.StepStatus.Failed,
                logInfo: `Error opening deep link`,
            });
        }
    }
}
exports.OpenLinkExecutor = OpenLinkExecutor;
async function openDeepLink(url, packageName, driver, platform, platformVersion) {
    switch (platform) {
        case mablApi_1.MobilePlatformEnum.Android:
            return driver.execute('mobile: deepLink', {
                url,
                package: packageName,
                waitForLaunch: false,
            });
        case mablApi_1.MobilePlatformEnum.Ios:
            let convertedNum = 16.4;
            try {
                convertedNum = Number(platformVersion);
            }
            catch (error) {
            }
            if (convertedNum < 16.4) {
                throw new Error(`Open Link is not currently supported on iOS versions lower than 16.4, current version is [${convertedNum}]`);
            }
            return driver.execute('mobile: deepLink', {
                url,
                bundleId: packageName,
            });
    }
}
function waitForPackageToBeActive(packageName, driver, platform) {
    return (0, async_retry_1.default)(async () => {
        switch (platform) {
            case mablApi_1.MobilePlatformEnum.Android:
                const currentPackage = await driver.execute('mobile: getCurrentPackage');
                if (currentPackage === packageName) {
                    return;
                }
                throw new Error(`Current active package [${currentPackage}] does not match expected [${packageName}]`);
            case mablApi_1.MobilePlatformEnum.Ios:
                const activeApp = await driver.execute('mobile: activeAppInfo');
                if (activeApp === packageName) {
                    return;
                }
                throw new Error(`Current active app [${activeApp?.bundleId}] does not match expected [${packageName}]`);
        }
    }, {
        retries: 10,
        minTimeout: 500,
        maxTimeout: 10_000,
        maxRetryTime: 10_000,
    });
}
