"use strict";
var sourcemap = require("source-map");
var path = require("path");
var IOSLogFilter = (function () {
    function IOSLogFilter($fs, $projectData, $loggingLevels) {
        this.$fs = $fs;
        this.$projectData = $projectData;
        this.$loggingLevels = $loggingLevels;
    }
    IOSLogFilter.prototype.filterData = function (data, logLevel, pid) {
        if (pid && data && data.indexOf("[" + pid + "]") === -1) {
            return null;
        }
        if (data) {
            if (data.indexOf("SecTaskCopyDebugDescription") !== -1 ||
                data.indexOf("NativeScript loaded bundle") !== -1 ||
                (data.indexOf("assertion failed:") !== -1 && data.indexOf("libxpc.dylib") !== -1)) {
                return null;
            }
            if (pid) {
                var searchString = "[" + pid + "]: ";
                var pidIndex = data.indexOf(searchString);
                if (pidIndex > 0) {
                    data = data.substring(pidIndex + searchString.length, data.length);
                    data = this.getOriginalFileLocation(data);
                }
            }
            return data.trim();
        }
        return data;
    };
    IOSLogFilter.prototype.getOriginalFileLocation = function (data) {
        var fileString = "file:///";
        var fileIndex = data.indexOf(fileString);
        if (fileIndex >= 0) {
            var parts = data.substring(fileIndex + fileString.length).split(":");
            if (parts.length >= 4) {
                var file = parts[0];
                var sourceMapFile = path.join(this.$projectData.projectDir, file + ".map");
                var row = parseInt(parts[1]);
                var column = parseInt(parts[2]);
                if (this.$fs.exists(sourceMapFile).wait()) {
                    var sourceMap = this.$fs.readText(sourceMapFile, "utf8").wait();
                    var smc = new sourcemap.SourceMapConsumer(sourceMap);
                    var originalPosition = smc.originalPositionFor({ line: row, column: column });
                    data = data.substring(0, fileIndex + fileString.length)
                        + file.replace(".js", ".ts") + ":"
                        + originalPosition.line + ":"
                        + originalPosition.column;
                    for (var i = 3; i < parts.length; i++) {
                        data += ":" + parts[i];
                    }
                }
            }
        }
        return data;
    };
    return IOSLogFilter;
}());
exports.IOSLogFilter = IOSLogFilter;
$injector.register("iOSLogFilter", IOSLogFilter);
