"use strict";
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");
const color_1 = require("../../../color");
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];
    }
    async execute(args) {
        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 = await this.$devicesService.getEmulatorImages({ platform });
            const emulators = this.$emulatorHelper.getEmulatorsFromAvailableEmulatorsOutput(availableEmulatorsOutput);
            devices.available = emulators;
            if (!this.$options.json) {
                this.$logger.info(color_1.color.bold("\n Available emulators"));
                this.printEmulators(emulators);
            }
        }
        let index = 1;
        await this.$devicesService.initialize({
            platform: args[0],
            deviceId: null,
            skipInferPlatform: true,
            skipDeviceDetectionInterval: true,
            skipEmulatorStart: true,
            fullDiscovery: true,
        });
        if (!this.$options.json) {
            this.$logger.info(color_1.color.bold("\n Connected devices & emulators"));
        }
        const table = (0, helpers_1.createTable)([
            "#",
            "Device Name",
            "Platform",
            "Device Identifier",
            "Type",
            "Status",
            "Connection Type",
        ], []);
        let action;
        if (this.$options.json) {
            action = async (device) => {
                devices.devices.push(device.deviceInfo);
            };
        }
        else {
            action = async (device) => {
                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(", "),
                ]);
            };
        }
        await 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(emulators) {
        const table = (0, helpers_1.createTable)([
            "Device Name",
            "Platform",
            "Version",
            "Device Identifier",
            "Image Identifier",
        ], []);
        for (const info of emulators) {
            table.push([
                info.displayName,
                info.platform,
                info.version,
                info.identifier || "",
                info.imageIdentifier || "",
            ]);
        }
        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 = [];
    }
    async execute(args) {
        const listDevicesCommand = this.$injector.resolve(ListDevicesCommand);
        const platform = this.$devicePlatformsConstants.Android;
        await 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 = [];
    }
    async execute(args) {
        const listDevicesCommand = this.$injector.resolve(ListDevicesCommand);
        const platform = this.$devicePlatformsConstants.iOS;
        await listDevicesCommand.execute([platform]);
    }
}
yok_1.injector.registerCommand(["device|ios", "devices|ios"], ListiOSDevicesCommand);
