"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 });
const net = require("net");
const _ = require("lodash");
const constants_1 = require("../../../constants");
const constants_2 = require("../../../../constants");
const yok_1 = require("../../../yok");
class IosEmulatorServices {
    constructor($logger, $devicePlatformsConstants, $iOSSimResolver, $mobileHelper) {
        this.$logger = $logger;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$iOSSimResolver = $iOSSimResolver;
        this.$mobileHelper = $mobileHelper;
    }
    startEmulator(options) {
        return __awaiter(this, void 0, void 0, function* () {
            let error = null;
            try {
                yield this.$iOSSimResolver.iOSSim.startSimulator({
                    device: options.imageIdentifier || options.emulatorIdOrName,
                    state: "None",
                    sdkVersion: options.sdk,
                });
            }
            catch (err) {
                error = err && err.message;
            }
            return {
                errors: error ? [error] : [],
            };
        });
    }
    getRunningEmulator() {
        return __awaiter(this, void 0, void 0, function* () {
            return null;
        });
    }
    getRunningEmulatorIds() {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    getRunningEmulatorName() {
        return __awaiter(this, void 0, void 0, function* () {
            return "";
        });
    }
    getRunningEmulatorImageIdentifier(emulatorId) {
        return __awaiter(this, void 0, void 0, function* () {
            return "";
        });
    }
    postDarwinNotification(notification, deviceId) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.$iOSSimResolver.iOSSim.sendNotification(notification, deviceId);
        });
    }
    connectToPort(data) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const socket = net.connect(data.port, "127.0.0.1");
                return socket;
            }
            catch (e) {
                this.$logger.trace(e);
            }
        });
    }
    getEmulatorImages() {
        return __awaiter(this, void 0, void 0, function* () {
            let devices = [];
            const errors = [];
            const output = yield this.tryGetiOSSimDevices();
            if (output.devices && output.devices.length) {
                devices = _(output.devices)
                    .map((simDevice) => this.convertSimDeviceToDeviceInfo(simDevice))
                    .sortBy((deviceInfo) => deviceInfo.version)
                    .value();
            }
            if (output.error) {
                errors.push(output.error);
            }
            return { devices, errors };
        });
    }
    getRunningEmulators() {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    tryGetiOSSimDevices() {
        return __awaiter(this, void 0, void 0, function* () {
            let devices = [];
            let error = null;
            try {
                devices = yield this.$iOSSimResolver.iOSSim.getDevices();
            }
            catch (err) {
                error = err;
            }
            return { devices, error };
        });
    }
    convertSimDeviceToDeviceInfo(simDevice) {
        return {
            imageIdentifier: simDevice.id,
            identifier: simDevice.id,
            displayName: simDevice.name,
            model: simDevice.name,
            version: simDevice.runtimeVersion,
            vendor: constants_1.APPLE_VENDOR_NAME,
            status: simDevice.state && simDevice.state.toLowerCase() === "booted"
                ? constants_1.RUNNING_EMULATOR_STATUS
                : constants_1.NOT_RUNNING_EMULATOR_STATUS,
            errorHelp: null,
            isTablet: this.$mobileHelper.isiOSTablet(simDevice.name),
            type: constants_1.DeviceTypes.Emulator,
            connectionTypes: [constants_2.DeviceConnectionType.Local],
            platform: this.$devicePlatformsConstants.iOS,
        };
    }
}
yok_1.injector.register("iOSEmulatorServices", IosEmulatorServices);
