"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 constants_1 = require("../common/constants");
const events_1 = require("events");
class IOSLogParserService extends events_1.EventEmitter {
    constructor($deviceLogProvider, $iosDeviceOperations, $iOSSimulatorLogProvider, $logger) {
        super();
        this.$deviceLogProvider = $deviceLogProvider;
        this.$iosDeviceOperations = $iosDeviceOperations;
        this.$iOSSimulatorLogProvider = $iOSSimulatorLogProvider;
        this.$logger = $logger;
        this.startedDeviceLogInstances = {};
    }
    startParsingLog(device, data) {
        return __awaiter(this, void 0, void 0, function* () {
            this.$deviceLogProvider.setProjectNameForDevice(device.deviceInfo.identifier, data.projectName);
            if (!this.startedDeviceLogInstances[device.deviceInfo.identifier]) {
                this.startParsingLogCore(device);
                yield this.startLogProcess(device);
                this.startedDeviceLogInstances[device.deviceInfo.identifier] = true;
            }
        });
    }
    startParsingLogCore(device) {
        const logProvider = device.isEmulator ? this.$iOSSimulatorLogProvider : this.$iosDeviceOperations;
        logProvider.on(constants_1.DEVICE_LOG_EVENT_NAME, (response) => this.processDeviceLogResponse(response));
    }
    processDeviceLogResponse(response) {
        const matches = IOSLogParserService.MESSAGE_REGEX.exec(response.message);
        if (matches) {
            const data = {
                port: parseInt(matches[1]),
                appId: matches[2],
                deviceId: response.deviceId
            };
            this.$logger.trace(`Emitting ${constants_1.DEBUGGER_PORT_FOUND_EVENT_NAME} event`, data);
            this.emit(constants_1.DEBUGGER_PORT_FOUND_EVENT_NAME, data);
        }
    }
    startLogProcess(device) {
        return __awaiter(this, void 0, void 0, function* () {
            if (device.isEmulator) {
                return this.$iOSSimulatorLogProvider.startNewMutedLogProcess(device.deviceInfo.identifier);
            }
            return this.$iosDeviceOperations.startDeviceLog(device.deviceInfo.identifier);
        });
    }
}
IOSLogParserService.MESSAGE_REGEX = /NativeScript debugger has opened inspector socket on port (\d+?) for (.*)[.]/;
exports.IOSLogParserService = IOSLogParserService;
$injector.register("iOSLogParserService", IOSLogParserService);
