"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.DeviceLogProviderBase = void 0;
const events_1 = require("events");
const helpers_1 = require("../helpers");
class DeviceLogProviderBase extends events_1.EventEmitter {
    constructor($logFilter, $logger, $logSourceMapService) {
        super();
        this.$logFilter = $logFilter;
        this.$logger = $logger;
        this.$logSourceMapService = $logSourceMapService;
        this.devicesLogOptions = {};
    }
    setSourceFileLocation(pathToOriginalFile) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                yield this.$logSourceMapService.setSourceMapConsumerForFile(pathToOriginalFile);
            }
            catch (err) {
                this.$logger.trace("Error while trying to set source map file", err);
            }
        });
    }
    setApplicationPidForDevice(deviceIdentifier, pid) {
        this.setDeviceLogOptionsProperty(deviceIdentifier, (deviceLogOptions) => deviceLogOptions.applicationPid, pid);
    }
    setProjectNameForDevice(deviceIdentifier, projectName) {
        this.setDeviceLogOptionsProperty(deviceIdentifier, (deviceLogOptions) => deviceLogOptions.projectName, projectName);
    }
    setProjectDirForDevice(deviceIdentifier, projectDir) {
        this.setDeviceLogOptionsProperty(deviceIdentifier, (deviceLogOptions) => deviceLogOptions.projectDir, projectDir);
    }
    setDefaultLogLevelForDevice(deviceIdentifier) {
        const logLevel = (this.devicesLogOptions[deviceIdentifier] &&
            this.devicesLogOptions[deviceIdentifier].logLevel) ||
            this.$logFilter.loggingLevel;
        this.setLogLevel(logLevel, deviceIdentifier);
    }
    getApplicationPidForDevice(deviceIdentifier) {
        return (this.devicesLogOptions[deviceIdentifier] &&
            this.devicesLogOptions[deviceIdentifier].applicationPid);
    }
    getDeviceLogOptionsForDevice(deviceIdentifier) {
        const loggingOptions = this.devicesLogOptions[deviceIdentifier];
        if (!loggingOptions) {
            this.setDefaultLogLevelForDevice(deviceIdentifier);
        }
        return this.devicesLogOptions[deviceIdentifier];
    }
    setDeviceLogOptionsProperty(deviceIdentifier, propNameFunction, propertyValue) {
        const propertyName = (0, helpers_1.getPropertyName)(propNameFunction);
        if (propertyName) {
            this.devicesLogOptions[deviceIdentifier] =
                this.devicesLogOptions[deviceIdentifier] ||
                    {};
            this.devicesLogOptions[deviceIdentifier][propertyName] = propertyValue;
        }
    }
}
exports.DeviceLogProviderBase = DeviceLogProviderBase;
