"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceEmitter = void 0;
const events_1 = require("events");
const constants_1 = require("../constants");
const yok_1 = require("../yok");
class DeviceEmitter extends events_1.EventEmitter {
    constructor($deviceLogProvider, $devicesService) {
        super();
        this.$deviceLogProvider = $deviceLogProvider;
        this.$devicesService = $devicesService;
        this.initialize();
    }
    initialize() {
        this.$devicesService.on(constants_1.DeviceDiscoveryEventNames.DEVICE_FOUND, (device) => {
            this.emit(constants_1.DeviceDiscoveryEventNames.DEVICE_FOUND, device.deviceInfo);
            this.attachApplicationChangedHandlers(device);
            // await: Do not await as this will require to mark the lambda with async keyword, but there's no way to await the lambda itself.
            /* tslint:disable:no-floating-promises */
            device.openDeviceLogStream();
            /* tslint:enable:no-floating-promises */
        });
        this.$devicesService.on(constants_1.DeviceDiscoveryEventNames.DEVICE_LOST, (device) => {
            this.emit(constants_1.DeviceDiscoveryEventNames.DEVICE_LOST, device.deviceInfo);
        });
        this.$devicesService.on(constants_1.DeviceDiscoveryEventNames.DEVICE_UPDATED, (device) => {
            this.emit(constants_1.DeviceDiscoveryEventNames.DEVICE_UPDATED, device.deviceInfo);
        });
        this.$deviceLogProvider.on("data", (identifier, data) => {
            this.emit(constants_1.DEVICE_LOG_EVENT_NAME, identifier, data.toString());
        });
        this.$devicesService.on(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_FOUND, (emulator) => {
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_FOUND, emulator);
        });
        this.$devicesService.on(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_LOST, (emulator) => {
            this.emit(constants_1.EmulatorDiscoveryNames.EMULATOR_IMAGE_LOST, emulator);
        });
    }
    attachApplicationChangedHandlers(device) {
        device.applicationManager.on("applicationInstalled", (appIdentifier) => {
            this.emit("applicationInstalled", device.deviceInfo.identifier, appIdentifier);
        });
        device.applicationManager.on("applicationUninstalled", (appIdentifier) => {
            this.emit("applicationUninstalled", device.deviceInfo.identifier, appIdentifier);
        });
        device.applicationManager.on("debuggableAppFound", (debuggableAppInfo) => {
            this.emit("debuggableAppFound", debuggableAppInfo);
        });
        device.applicationManager.on("debuggableAppLost", (debuggableAppInfo) => {
            this.emit("debuggableAppLost", debuggableAppInfo);
        });
        device.applicationManager.on("debuggableViewFound", (appIdentifier, debuggableWebViewInfo) => {
            this.emit("debuggableViewFound", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo);
        });
        device.applicationManager.on("debuggableViewLost", (appIdentifier, debuggableWebViewInfo) => {
            this.emit("debuggableViewLost", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo);
        });
        device.applicationManager.on("debuggableViewChanged", (appIdentifier, debuggableWebViewInfo) => {
            this.emit("debuggableViewChanged", device.deviceInfo.identifier, appIdentifier, debuggableWebViewInfo);
        });
    }
}
exports.DeviceEmitter = DeviceEmitter;
yok_1.injector.register("deviceEmitter", DeviceEmitter);
//# sourceMappingURL=device-emitter.js.map