"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AndroidLogFilter = void 0;
const yok_1 = require("../../yok");
const os_1 = require("os");
class AndroidLogFilter {
    constructor($loggingLevels) {
        this.$loggingLevels = $loggingLevels;
    }
    filterData(data, loggingOptions = {}) {
        const specifiedLogLevel = (loggingOptions.logLevel || "").toUpperCase();
        if (specifiedLogLevel === this.$loggingLevels.info) {
            const log = this.getConsoleLogFromLine(data, loggingOptions.applicationPid);
            if (log) {
                if (log.tag) {
                    return (`${log.tag === "JS" ? "" : `${log.tag}: `}${log.message}` + os_1.EOL);
                }
                else {
                    return log.message + os_1.EOL;
                }
            }
            return null;
        }
        return data + os_1.EOL;
    }
    getConsoleLogFromLine(lineText, pid) {
        if (pid && lineText.indexOf(pid) === -1) {
            return null;
        }
        let consoleLogMessage;
        const match = lineText.match(AndroidLogFilter.API_LEVEL_23_LINE_REGEX) ||
            lineText.match(AndroidLogFilter.LINE_REGEX);
        if (match) {
            consoleLogMessage = { tag: match[1].trim(), message: match[2] };
        }
        return consoleLogMessage;
    }
}
AndroidLogFilter.LINE_REGEX = /.\/(.+?)\s*\(\s*\d+?\): (.*)/;
AndroidLogFilter.API_LEVEL_23_LINE_REGEX = /.+?\s+?(?:[A-Z]\s+?)([A-Za-z \.]+?)\s*?\: (.*)/;
exports.AndroidLogFilter = AndroidLogFilter;
yok_1.injector.register("androidLogFilter", AndroidLogFilter);
