"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("../../helpers");
class ListDevicesCommand {
    constructor($devicesService, $errors, $emulatorHelper, $logger, $stringParameter, $mobileHelper, $options) {
        this.$devicesService = $devicesService;
        this.$errors = $errors;
        this.$emulatorHelper = $emulatorHelper;
        this.$logger = $logger;
        this.$stringParameter = $stringParameter;
        this.$mobileHelper = $mobileHelper;
        this.$options = $options;
        this.allowedParameters = [this.$stringParameter];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$options.availableDevices) {
                const platform = this.$mobileHelper.normalizePlatformName(args[0]);
                if (!platform && args[0]) {
                    this.$errors.fail(`${args[0]} is not a valid device platform. The valid platforms are ${helpers_1.formatListOfNames(this.$mobileHelper.platformNames)}`);
                }
                const availableEmulatorsOutput = yield this.$devicesService.getEmulatorImages({ platform });
                const emulators = this.$emulatorHelper.getEmulatorsFromAvailableEmulatorsOutput(availableEmulatorsOutput);
                this.printEmulators("\nAvailable emulators", emulators);
            }
            this.$logger.info("\nConnected devices & emulators");
            let index = 1;
            yield this.$devicesService.initialize({ platform: args[0], deviceId: null, skipInferPlatform: true, skipDeviceDetectionInterval: true, skipEmulatorStart: true });
            const table = helpers_1.createTable(["#", "Device Name", "Platform", "Device Identifier", "Type", "Status"], []);
            let action;
            if (this.$options.json) {
                action = (device) => __awaiter(this, void 0, void 0, function* () {
                    this.$logger.info(JSON.stringify(device.deviceInfo));
                });
            }
            else {
                action = (device) => __awaiter(this, void 0, void 0, function* () {
                    table.push([(index++).toString(), device.deviceInfo.displayName || '',
                        device.deviceInfo.platform || '', device.deviceInfo.identifier || '',
                        device.deviceInfo.type || '', device.deviceInfo.status || '']);
                });
            }
            yield this.$devicesService.execute(action, undefined, { allowNoDevices: true });
            if (!this.$options.json && table.length) {
                this.$logger.info(table.toString());
            }
        });
    }
    printEmulators(title, emulators) {
        this.$logger.info(title);
        const table = helpers_1.createTable(["Device Name", "Platform", "Version", "Device Identifier", "Image Identifier", "Error Help"], []);
        for (const info of emulators) {
            table.push([info.displayName, info.platform, info.version, info.identifier || "", info.imageIdentifier || "", info.errorHelp || ""]);
        }
        this.$logger.info(table.toString());
    }
}
exports.ListDevicesCommand = ListDevicesCommand;
$injector.registerCommand(["device|*list", "devices|*list"], ListDevicesCommand);
class ListAndroidDevicesCommand {
    constructor($injector, $devicePlatformsConstants) {
        this.$injector = $injector;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const listDevicesCommand = this.$injector.resolve(ListDevicesCommand);
            const platform = this.$devicePlatformsConstants.Android;
            yield listDevicesCommand.execute([platform]);
        });
    }
}
$injector.registerCommand(["device|android", "devices|android"], ListAndroidDevicesCommand);
class ListiOSDevicesCommand {
    constructor($injector, $devicePlatformsConstants) {
        this.$injector = $injector;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.allowedParameters = [];
    }
    execute(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const listDevicesCommand = this.$injector.resolve(ListDevicesCommand);
            const platform = this.$devicePlatformsConstants.iOS;
            yield listDevicesCommand.execute([platform]);
        });
    }
}
$injector.registerCommand(["device|ios", "devices|ios"], ListiOSDevicesCommand);
