"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const constants_1 = require("../../../../common/constants");
class PreviewDevicesService extends events_1.EventEmitter {
    constructor($previewAppLogProvider, $previewAppPluginsService) {
        super();
        this.$previewAppLogProvider = $previewAppLogProvider;
        this.$previewAppPluginsService = $previewAppPluginsService;
        this.connectedDevices = [];
        this.initialize();
    }
    getConnectedDevices() {
        return this.connectedDevices;
    }
    updateConnectedDevices(devices) {
        _(devices)
            .reject(d => _.find(this.connectedDevices, device => d.id === device.id))
            .each(device => this.raiseDeviceFound(device));
        _(this.connectedDevices)
            .reject(d => _.find(devices, device => d.id === device.id))
            .each(device => this.raiseDeviceLost(device));
    }
    getDeviceById(id) {
        return _.find(this.connectedDevices, { id });
    }
    getDevicesForPlatform(platform) {
        return _.filter(this.connectedDevices, { platform: platform.toLowerCase() });
    }
    getPluginsUsageWarnings(data, device) {
        return this.$previewAppPluginsService.getPluginsUsageWarnings(data, device);
    }
    initialize() {
        this.$previewAppLogProvider.on(constants_1.DEVICE_LOG_EVENT_NAME, (deviceId, message) => {
            this.emit(constants_1.DEVICE_LOG_EVENT_NAME, deviceId, message);
        });
    }
    raiseDeviceFound(device) {
        this.emit(constants_1.DeviceDiscoveryEventNames.DEVICE_FOUND, device);
        this.connectedDevices.push(device);
    }
    raiseDeviceLost(device) {
        this.emit(constants_1.DeviceDiscoveryEventNames.DEVICE_LOST, device);
        _.remove(this.connectedDevices, d => d.id === device.id);
    }
}
exports.PreviewDevicesService = PreviewDevicesService;
$injector.register("previewDevicesService", PreviewDevicesService);
