"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.IOSSimulatorDiscovery = void 0;
const device_discovery_1 = require("./device-discovery");
const ios_simulator_device_1 = require("./../ios/simulator/ios-simulator-device");
const constants_1 = require("../../constants");
const yok_1 = require("../../yok");
const _ = require("lodash");
class IOSSimulatorDiscovery extends device_discovery_1.DeviceDiscovery {
    constructor($injector, $iOSSimResolver, $mobileHelper, $hostInfo, $iOSEmulatorServices) {
        super();
        this.$injector = $injector;
        this.$iOSSimResolver = $iOSSimResolver;
        this.$mobileHelper = $mobileHelper;
        this.$hostInfo = $hostInfo;
        this.$iOSEmulatorServices = $iOSEmulatorServices;
        this.cachedSimulators = [];
        this.availableSimulators = {};
    }
    startLookingForDevices(options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options &&
                options.platform &&
                !this.$mobileHelper.isiOSPlatform(options.platform)) {
                return;
            }
            return this.checkForDevices();
        });
    }
    checkForDevices() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.$hostInfo.isDarwin) {
                const currentSimulators = yield this.$iOSSimResolver.iOSSim.getRunningSimulators();
                // Remove old simulators
                _(this.cachedSimulators)
                    .reject((s) => _.some(currentSimulators, (simulator) => simulator &&
                    s &&
                    simulator.id === s.id &&
                    simulator.state === s.state))
                    .each((s) => this.deleteAndRemoveDevice(s));
                // Add new simulators
                _(currentSimulators)
                    .reject((s) => _.some(this.cachedSimulators, (simulator) => simulator &&
                    s &&
                    simulator.id === s.id &&
                    simulator.state === s.state))
                    .each((s) => this.createAndAddDevice(s));
            }
        });
    }
    checkForAvailableSimulators() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.$hostInfo.isDarwin) {
                return [];
            }
            const simulators = (yield this.$iOSEmulatorServices.getEmulatorImages())
                .devices;
            const currentSimulators = _.values(this.availableSimulators);
            const lostSimulators = [];
            const foundSimulators = [];
            for (const simulator of currentSimulators) {
                if (!_.find(this.availableSimulators, (s) => s.imageIdentifier === simulator.imageIdentifier)) {
                    lostSimulators.push(simulator);
                }
            }
            for (const simulator of simulators) {
                if (!this.availableSimulators[simulator.imageIdentifier]) {
                    foundSimulators.push(simulator);
                }
            }
            if (lostSimulators.length) {
                this.raiseOnEmulatorImagesLost(lostSimulators);
            }
            if (foundSimulators.length) {
                this.raiseOnEmulatorImagesFound(foundSimulators);
            }
            return simulators;
        });
    }
    createAndAddDevice(simulator) {
        this.cachedSimulators.push(_.cloneDeep(simulator));
        this.addDevice(this.$injector.resolve(ios_simulator_device_1.IOSSimulator, { simulator: simulator }));
    }
    deleteAndRemoveDevice(simulator) {
        _.remove(this.cachedSimulators, (s) => s && s.id === simulator.id);
        this.removeDevice(simulator.id);
    }
    raiseOnEmulatorImagesFound(simulators) {
        _.forEach(simulators, (simulator) => {
            this.availableSimulators[simulator.imageIdentifier] = simulator;
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_FOUND, simulator);
        });
    }
    raiseOnEmulatorImagesLost(simulators) {
        _.forEach(simulators, (simulator) => {
            delete this.availableSimulators[simulator.imageIdentifier];
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_LOST, simulator);
        });
    }
}
exports.IOSSimulatorDiscovery = IOSSimulatorDiscovery;
yok_1.injector.register("iOSSimulatorDiscovery", IOSSimulatorDiscovery);
//# sourceMappingURL=ios-simulator-discovery.js.map