"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var events_1 = require("events");
var DeviceEmitter = (function (_super) {
    __extends(DeviceEmitter, _super);
    function DeviceEmitter($androidDeviceDiscovery, $iOSDeviceDiscovery, $iOSSimulatorDiscovery, $devicesService, $deviceLogProvider, $companionAppsService, $projectConstants, $devicePlatformsConstants) {
        _super.call(this);
        this.$androidDeviceDiscovery = $androidDeviceDiscovery;
        this.$iOSDeviceDiscovery = $iOSDeviceDiscovery;
        this.$iOSSimulatorDiscovery = $iOSSimulatorDiscovery;
        this.$devicesService = $devicesService;
        this.$deviceLogProvider = $deviceLogProvider;
        this.$companionAppsService = $companionAppsService;
        this.$projectConstants = $projectConstants;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
    }
    Object.defineProperty(DeviceEmitter.prototype, "companionAppIdentifiers", {
        get: function () {
            if (!this._companionAppIdentifiers) {
                this._companionAppIdentifiers = this.$companionAppsService.getAllCompanionAppIdentifiers();
            }
            return this._companionAppIdentifiers;
        },
        enumerable: true,
        configurable: true
    });
    DeviceEmitter.prototype.initialize = function () {
        var _this = this;
        return (function () {
            _this.$androidDeviceDiscovery.ensureAdbServerStarted().wait();
            _this.$androidDeviceDiscovery.on("deviceFound", function (device) {
                _this.emit("deviceFound", device.deviceInfo);
                _this.attachApplicationChangedHandlers(device);
                device.openDeviceLogStream();
            });
            _this.$androidDeviceDiscovery.on("deviceLost", function (device) {
                _this.emit("deviceLost", device.deviceInfo);
            });
            _this.$iOSDeviceDiscovery.on("deviceFound", function (device) {
                _this.emit("deviceFound", device.deviceInfo);
                _this.attachApplicationChangedHandlers(device);
                device.openDeviceLogStream();
            });
            _this.$iOSDeviceDiscovery.on("deviceLost", function (device) {
                _this.emit("deviceLost", device.deviceInfo);
            });
            _this.$iOSSimulatorDiscovery.on("deviceFound", function (device) {
                _this.emit("deviceFound", device.deviceInfo);
                device.openDeviceLogStream();
                _this.attachApplicationChangedHandlers(device);
            });
            _this.$iOSSimulatorDiscovery.on("deviceLost", function (device) {
                _this.emit("deviceLost", device.deviceInfo);
            });
            _this.$devicesService.initialize({ skipInferPlatform: true }).wait();
            _this.$deviceLogProvider.on("data", function (identifier, data) {
                _this.emit('deviceLogData', identifier, data.toString());
            });
        }).future()();
    };
    DeviceEmitter.prototype.attachApplicationChangedHandlers = function (device) {
        var _this = this;
        device.applicationManager.on("applicationInstalled", function (applicationName) {
            _this.emit("applicationInstalled", device.deviceInfo.identifier, applicationName);
            _this.checkCompanionAppChanged(device, applicationName, "companionAppInstalled");
        });
        device.applicationManager.on("applicationUninstalled", function (applicationName) {
            _this.emit("applicationUninstalled", device.deviceInfo.identifier, applicationName);
            _this.checkCompanionAppChanged(device, applicationName, "companionAppUninstalled");
        });
    };
    DeviceEmitter.prototype.checkCompanionAppChanged = function (device, applicationName, eventName) {
        var _this = this;
        var devicePlatform = device.deviceInfo.platform.toLowerCase();
        _.each(this.companionAppIdentifiers, function (platformsCompanionAppIdentifiers, framework) {
            if (applicationName === platformsCompanionAppIdentifiers[devicePlatform]) {
                _this.emit(eventName, device.deviceInfo.identifier, framework);
                return false;
            }
        });
    };
    return DeviceEmitter;
})(events_1.EventEmitter);
$injector.register("deviceEmitter", DeviceEmitter);
