text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  const events_1 = require("events");
  const constants_1 = require("../../constants");
  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);
              const lostEmulators = _(cachedEmulators)
                  .reject(e => _.some(currentEmulators, emulator => emulator && e && emulator.imageIdentifier === e.imageIdentifier))
                  .value();
              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;
  $injector.register("androidEmulatorDiscovery", AndroidEmulatorDiscovery);
  