"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ListDevicesCommand = void 0;
const helpers_1 = require("../../helpers");
const constants_1 = require("../../../constants");
const yok_1 = require("../../yok");
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* () {
            const devices = {
                devices: [],
            };
            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 ${(0, helpers_1.formatListOfNames)(this.$mobileHelper.platformNames)}`);
                }
                const availableEmulatorsOutput = yield this.$devicesService.getEmulatorImages({ platform });
                const emulators = this.$emulatorHelper.getEmulatorsFromAvailableEmulatorsOutput(availableEmulatorsOutput);
                devices.available = emulators;
                if (!this.$options.json) {
                    this.printEmulators("\nAvailable emulators", emulators);
                }
            }
            if (!this.$options.json) {
                this.$logger.info("\nConnected devices & emulators");
            }
            let index = 1;
            yield this.$devicesService.initialize({
                platform: args[0],
                deviceId: null,
                skipInferPlatform: true,
                skipDeviceDetectionInterval: true,
                skipEmulatorStart: true,
                fullDiscovery: true,
            });
            const table = (0, helpers_1.createTable)([
                "#",
                "Device Name",
                "Platform",
                "Device Identifier",
                "Type",
                "Status",
                "Connection Type",
            ], []);
            let action;
            if (this.$options.json) {
                action = (device) => __awaiter(this, void 0, void 0, function* () {
                    devices.devices.push(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 || "",
                        device.deviceInfo.connectionTypes
                            .map((type) => constants_1.DeviceConnectionType[type])
                            .join(", "),
                    ]);
                });
            }
            yield this.$devicesService.execute(action, undefined, {
                allowNoDevices: true,
            });
            if (this.$options.json) {
                return this.$logger.info(JSON.stringify(devices, null, 2));
            }
            if (table.length) {
                this.$logger.info(table.toString());
            }
        });
    }
    printEmulators(title, emulators) {
        this.$logger.info(title);
        const table = (0, 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;
yok_1.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]);
        });
    }
}
yok_1.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]);
        });
    }
}
yok_1.injector.registerCommand(["device|ios", "devices|ios"], ListiOSDevicesCommand);
