"use strict";
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;
    }
    async startEmulator(options) {
        var _a;
        let error = null;
        try {
            if (options.platform === this.$devicePlatformsConstants.visionOS &&
                !options.emulatorIdOrName) {
                const { devices } = await this.tryGetiOSSimDevices();
                options.emulatorIdOrName = (_a = devices.find((device) => device.platform === this.$devicePlatformsConstants.visionOS)) === null || _a === void 0 ? void 0 : _a.id;
            }
            await this.$iOSSimResolver.iOSSim.startSimulator({
                device: options.imageIdentifier || options.emulatorIdOrName,
                state: "None",
                sdkVersion: options.sdk,
            });
        }
        catch (err) {
            error = err && err.message;
        }
        return {
            errors: error ? [error] : [],
        };
    }
    async getRunningEmulator() {
        return null;
    }
    async getRunningEmulatorIds() {
        return [];
    }
    async getRunningEmulatorName() {
        return "";
    }
    async getRunningEmulatorImageIdentifier(emulatorId) {
        return "";
    }
    async postDarwinNotification(notification, deviceId) {
        return this.$iOSSimResolver.iOSSim.sendNotification(notification, deviceId);
    }
    async connectToPort(data) {
        try {
            const socket = net.connect(data.port, "127.0.0.1");
            return socket;
        }
        catch (e) {
            this.$logger.trace(e);
        }
    }
    async getEmulatorImages() {
        let devices = [];
        const errors = [];
        const output = await 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 };
    }
    async getRunningEmulators() {
        return [];
    }
    async tryGetiOSSimDevices() {
        let devices = [];
        let error = null;
        try {
            devices = await this.$iOSSimResolver.iOSSim.getDevices();
        }
        catch (err) {
            error = err;
        }
        return { devices, error };
    }
    convertSimDeviceToDeviceInfo(simDevice) {
        var _a;
        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: (_a = simDevice.platform) !== null && _a !== void 0 ? _a : this.$devicePlatformsConstants.iOS,
        };
    }
}
yok_1.injector.register("iOSEmulatorServices", IosEmulatorServices);
