"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.AndroidEmulatorDiscovery = void 0;
const events_1 = require("events");
const constants_1 = require("../../constants");
const _ = require("lodash");
const yok_1 = require("../../yok");
class AndroidEmulatorDiscovery extends events_1.EventEmitter {
    constructor($androidEmulatorServices, $mobileHelper) {
        super();
        this.$androidEmulatorServices = $androidEmulatorServices;
        this.$mobileHelper = $mobileHelper;
        this._emulators = {};
    }
    startLookingForDevices(options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (options &&
                options.platform &&
                !this.$mobileHelper.isAndroidPlatform(options.platform)) {
                return;
            }
            const availableEmulatorsOutput = yield this.$androidEmulatorServices.getEmulatorImages();
            const currentEmulators = availableEmulatorsOutput.devices;
            const cachedEmulators = _.values(this._emulators);
            // Remove old emulators
            const lostEmulators = _(cachedEmulators)
                .reject((e) => _.some(currentEmulators, (emulator) => emulator && e && emulator.imageIdentifier === e.imageIdentifier))
                .value();
            // Add new emulators
            const foundEmulators = _(currentEmulators)
                .reject((e) => _.some(cachedEmulators, (emulator) => emulator && e && emulator.imageIdentifier === e.imageIdentifier))
                .value();
            if (lostEmulators.length) {
                this.raiseOnEmulatorImagesLost(lostEmulators);
            }
            if (foundEmulators.length) {
                this.raiseOnEmulatorImagesFound(foundEmulators);
            }
        });
    }
    getDevices() {
        return _.values(this._emulators);
    }
    raiseOnEmulatorImagesFound(emulators) {
        _.forEach(emulators, (emulator) => {
            this._emulators[emulator.imageIdentifier] = emulator;
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_FOUND, emulator);
        });
    }
    raiseOnEmulatorImagesLost(emulators) {
        _.forEach(emulators, (emulator) => {
            delete this._emulators[emulator.imageIdentifier];
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_LOST, emulator);
        });
    }
}
exports.AndroidEmulatorDiscovery = AndroidEmulatorDiscovery;
yok_1.injector.register("androidEmulatorDiscovery", AndroidEmulatorDiscovery);
//# sourceMappingURL=android-emulator-discovery.js.map