"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 byline = require("byline");
const device_android_debug_bridge_1 = require("./device-android-debug-bridge");
const semver = require("semver");
class LogcatHelper {
    constructor($deviceLogProvider, $devicePlatformsConstants, $logger, $injector, $processService, $devicesService) {
        this.$deviceLogProvider = $deviceLogProvider;
        this.$devicePlatformsConstants = $devicePlatformsConstants;
        this.$logger = $logger;
        this.$injector = $injector;
        this.$processService = $processService;
        this.$devicesService = $devicesService;
        this.mapDevicesLoggingData = Object.create(null);
    }
    start(options) {
        return __awaiter(this, void 0, void 0, function* () {
            const deviceIdentifier = options.deviceIdentifier;
            if (deviceIdentifier && !this.mapDevicesLoggingData[deviceIdentifier]) {
                this.mapDevicesLoggingData[deviceIdentifier] = {
                    loggingProcess: null,
                    lineStream: null,
                    keepSingleProcess: options.keepSingleProcess
                };
                const logcatStream = yield this.getLogcatStream(deviceIdentifier, options.pid);
                const lineStream = byline(logcatStream.stdout);
                this.mapDevicesLoggingData[deviceIdentifier].loggingProcess = logcatStream;
                this.mapDevicesLoggingData[deviceIdentifier].lineStream = lineStream;
                logcatStream.stderr.on("data", (data) => {
                    this.$logger.trace("ADB logcat stderr: " + data.toString());
                });
                logcatStream.on("close", (code) => {
                    try {
                        this.stop(deviceIdentifier);
                        if (code !== 0) {
                            this.$logger.trace("ADB process exited with code " + code.toString());
                        }
                    }
                    catch (err) {
                    }
                });
                lineStream.on('data', (line) => {
                    const lineText = line.toString();
                    this.$deviceLogProvider.logData(lineText, this.$devicePlatformsConstants.Android, deviceIdentifier);
                });
                this.$processService.attachToProcessExitSignals(this, logcatStream.kill);
            }
        });
    }
    getLogcatStream(deviceIdentifier, pid) {
        return __awaiter(this, void 0, void 0, function* () {
            const device = yield this.$devicesService.getDevice(deviceIdentifier);
            const minAndroidWithLogcatPidSupport = "7.0.0";
            const isLogcatPidSupported = semver.gte(semver.coerce(device.deviceInfo.version), minAndroidWithLogcatPidSupport);
            const adb = this.$injector.resolve(device_android_debug_bridge_1.DeviceAndroidDebugBridge, { identifier: deviceIdentifier });
            const logcatCommand = ["logcat"];
            if (pid && isLogcatPidSupported) {
                logcatCommand.push(`--pid=${pid}`);
            }
            const logcatStream = yield adb.executeCommand(logcatCommand, { returnChildProcess: true });
            return logcatStream;
        });
    }
    stop(deviceIdentifier) {
        if (this.mapDevicesLoggingData[deviceIdentifier] && !this.mapDevicesLoggingData[deviceIdentifier].keepSingleProcess) {
            this.mapDevicesLoggingData[deviceIdentifier].loggingProcess.removeAllListeners();
            this.mapDevicesLoggingData[deviceIdentifier].loggingProcess.kill("SIGINT");
            this.mapDevicesLoggingData[deviceIdentifier].lineStream.removeAllListeners();
            delete this.mapDevicesLoggingData[deviceIdentifier];
        }
    }
}
exports.LogcatHelper = LogcatHelper;
$injector.register("logcatHelper", LogcatHelper);
