"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IOSLogFilter = void 0;
const yok_1 = require("../common/yok");
class IOSLogFilter {
    constructor($loggingLevels) {
        this.$loggingLevels = $loggingLevels;
        this.appOutputRegex = /([^\s\(\)]+)(?:\(([^\s]+)\))?\[[0-9]+\]/;
        this.infoFilterRegex = new RegExp(`^.*(?:<Notice>:|<Error>:|<Warning>:|\\(NativeScript\\)|${this.appOutputRegex.source}:){1}`);
        this.postFilterRegex = /^\((.+)\) \[com\.apple.+\]/;
        this.filterActive = true;
        this.partialLine = null;
    }
    filterData(data, loggingOptions = {}) {
        const specifiedLogLevel = (loggingOptions.logLevel || "").toUpperCase();
        if (specifiedLogLevel !== this.$loggingLevels.info || !data) {
            return data;
        }
        const chunkLines = data.split("\n");
        const skipLastLine = chunkLines.length > 0 ? data[data.length - 1] !== "\n" : false;
        let output = "";
        for (let i = 0; i < chunkLines.length; i++) {
            let currentLine = chunkLines[i];
            if (this.partialLine) {
                currentLine = this.partialLine + currentLine;
                this.partialLine = undefined;
            }
            if (i === chunkLines.length - 1 && skipLastLine) {
                this.partialLine = currentLine;
                break;
            }
            if (this.preFilter(data, currentLine)) {
                continue;
            }
            const matchResult = this.appOutputRegex.exec(currentLine);
            if (matchResult && matchResult.length > 1) {
                const projectName = loggingOptions && loggingOptions.projectName;
                this.filterActive = matchResult[1] !== projectName;
                if (matchResult === null || matchResult === void 0 ? void 0 : matchResult[2]) {
                    this.filterActive || (this.filterActive = matchResult[2] !== "NativeScript");
                }
            }
            if (this.filterActive) {
                continue;
            }
            const filteredLineInfo = currentLine.match(this.infoFilterRegex);
            if (filteredLineInfo && filteredLineInfo.length > 0) {
                currentLine = currentLine.replace(filteredLineInfo[0], "");
            }
            currentLine = currentLine.trim();
            const postFilterMatch = this.postFilterRegex.exec(currentLine);
            if (postFilterMatch && (postFilterMatch === null || postFilterMatch === void 0 ? void 0 : postFilterMatch[1]) !== "NativeScript") {
                continue;
            }
            output += currentLine + "\n";
        }
        return output.length === 0 ? null : output;
    }
    preFilter(data, currentLine) {
        return (currentLine.length < 1 ||
            currentLine.indexOf("SecTaskCopyDebugDescription") !== -1 ||
            currentLine.indexOf("NativeScript loaded bundle") !== -1 ||
            (currentLine.indexOf("assertion failed:") !== -1 &&
                data.indexOf("libxpc.dylib") !== -1));
    }
}
exports.IOSLogFilter = IOSLogFilter;
yok_1.injector.register("iOSLogFilter", IOSLogFilter);
