"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.IOSSimulatorLogProvider = void 0;
const events_1 = require("events");
const _ = require("lodash");
const yok_1 = require("../../../yok");
class IOSSimulatorLogProvider extends events_1.EventEmitter {
    constructor($iOSSimResolver, $logger, $devicePlatformsConstants, $deviceLogProvider) {
        super();
        this.$iOSSimResolver = $iOSSimResolver;
        this.$logger = $logger;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$deviceLogProvider = $deviceLogProvider;
        this.simulatorsLoggingEnabled = {};
        this.simulatorsLogProcess = {};
        this.shouldDispose = true;
    }
    setShouldDispose(shouldDispose) {
        this.shouldDispose = shouldDispose;
    }
    startLogProcess(deviceId, options) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.simulatorsLoggingEnabled[deviceId]) {
                const deviceLogChildProcess = yield this.$iOSSimResolver.iOSSim.getDeviceLogProcess(deviceId, options ? options.predicate : null);
                const action = (data) => {
                    const message = data.toString();
                    this.$deviceLogProvider.logData(message, this.$devicePlatformsConstants.iOS, deviceId);
                };
                if (deviceLogChildProcess) {
                    deviceLogChildProcess.once("close", () => {
                        this.simulatorsLoggingEnabled[deviceId] = false;
                    });
                    deviceLogChildProcess.once("error", (err) => {
                        this.$logger.trace(`Error is thrown for device with identifier ${deviceId}. More info: ${err.message}.`);
                        this.simulatorsLoggingEnabled[deviceId] = false;
                    });
                }
                if (deviceLogChildProcess.stdout) {
                    deviceLogChildProcess.stdout.on("data", action.bind(this));
                }
                if (deviceLogChildProcess.stderr) {
                    deviceLogChildProcess.stderr.on("data", action.bind(this));
                }
                this.simulatorsLoggingEnabled[deviceId] = true;
                this.simulatorsLogProcess[deviceId] = deviceLogChildProcess;
            }
        });
    }
    dispose(signal) {
        if (this.shouldDispose) {
            _.each(this.simulatorsLogProcess, (logProcess, deviceId) => {
                if (logProcess) {
                    logProcess.kill(signal);
                }
            });
        }
    }
}
exports.IOSSimulatorLogProvider = IOSSimulatorLogProvider;
yok_1.injector.register("iOSSimulatorLogProvider", IOSSimulatorLogProvider);
//# sourceMappingURL=ios-simulator-log-provider.js.map