"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddOnOptions = void 0;
const child_process_1 = require("child_process");
const loggingProvider_1 = require("@mablhq-internal/runtime/providers/logging/loggingProvider");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const resourceUtil_1 = require("@mablhq-internal/runtime/util/resourceUtil");
const OperatingSystemDescriptor_1 = require("@mablhq/test-model/types/OperatingSystemDescriptor");
const UI_AUTOMATOR_VERSION = '7.0.0';
const XCUITEST_VERSION = '10.24.1';
const addOnConfigKey = 'add-on';
var AddOnOptions;
(function (AddOnOptions) {
    AddOnOptions["browser"] = "browser";
    AddOnOptions["mobile"] = "mobile-tools";
})(AddOnOptions || (exports.AddOnOptions = AddOnOptions = {}));
exports.command = `install <${addOnConfigKey}>`;
exports.describe = 'Install add-on tools like mobile testing drivers';
exports.builder = (yargs) => {
    yargs
        .positional(addOnConfigKey, {
        describe: 'add-on to install',
        type: 'string',
        choices: [AddOnOptions.mobile, AddOnOptions.browser],
    })
        .option('android', {
        describe: `[${AddOnOptions.mobile}] The android driver version to use`,
        type: 'string',
    })
        .option('ios', {
        describe: `[${AddOnOptions.mobile}] The ios driver version to use`,
        type: 'string',
    })
        .option('chrome', {
        describe: `[${AddOnOptions.browser}] The chrome browser version to use`,
        type: 'boolean',
    })
        .option('webkit', {
        describe: `[${AddOnOptions.browser}] The webkit browser version to use`,
        type: 'boolean',
        hidden: true,
    })
        .option('firefox', {
        describe: `[${AddOnOptions.browser}] The firefox browser version to use`,
        type: 'boolean',
        hidden: true,
    });
};
exports.handler = installAddOn;
async function installAddOn(parsed) {
    const addOn = parsed[addOnConfigKey];
    const iosVersion = parsed.ios ?? XCUITEST_VERSION;
    const androidVersion = parsed.android ?? UI_AUTOMATOR_VERSION;
    switch (addOn) {
        case AddOnOptions.mobile:
            loggingProvider_1.logger.info('Installing mobile add on tools');
            const operatingSystem = (0, OperatingSystemDescriptor_1.getOperatingSystem)();
            const parentDirOfNodeModulesDir = (0, resourceUtil_1.findNodeModulesDirectories)()[0];
            switch (operatingSystem) {
                case OperatingSystemDescriptor_1.OperatingSystem.MACOS:
                    loggingProvider_1.logger.info(`android version: ${androidVersion}`);
                    loggingProvider_1.logger.info(`ios version: ${iosVersion}`);
                    (0, child_process_1.execSync)(`npm install appium-uiautomator2-driver@${androidVersion} appium-xcuitest-driver@${iosVersion}`, { cwd: parentDirOfNodeModulesDir });
                    break;
                default:
                    loggingProvider_1.logger.info(`android version: ${androidVersion}`);
                    loggingProvider_1.logger.info(`ios driver only supported on Mac OS, ${operatingSystem} platform detected`);
                    (0, child_process_1.execSync)(`npm install appium-uiautomator2-driver@${androidVersion}`, {
                        cwd: parentDirOfNodeModulesDir,
                    });
            }
            console.log(`${parentDirOfNodeModulesDir}/node_modules/.cache/appium/extensions.yaml`);
            const pathToExtenstionsFile = path.resolve(`${parentDirOfNodeModulesDir}/node_modules/.cache/appium/extensions.yaml`);
            if (fs.existsSync(pathToExtenstionsFile)) {
                fs.unlinkSync(pathToExtenstionsFile);
            }
            loggingProvider_1.logger.info('Installation of mobile add on tools complete');
            return;
        case AddOnOptions.browser:
            const browser = parsed.chrome
                ? 'chrome'
                : parsed.webkit
                    ? 'webkit'
                    : 'firefox';
            loggingProvider_1.logger.info(`Installing ${browser} browser`);
            await require('playwright-core/lib/server').installBrowsersForNpmInstall([
                browser,
            ]);
            loggingProvider_1.logger.info('Installation of browser add on tools complete');
            return;
        default:
            throw new Error(`Invalid add on provided [${addOn}]`);
    }
}
