"use strict";
var minimatch = require("minimatch");
var PathFilteringService = (function () {
    function PathFilteringService($fs) {
        this.$fs = $fs;
    }
    PathFilteringService.prototype.getRulesFromFile = function (fullFilePath) {
        var COMMENT_START = '#';
        var rules = [];
        try {
            var fileContent = this.$fs.readText(fullFilePath).wait();
            rules = _.reject(fileContent.split(/[\n\r]/), function (line) { return line.length === 0 || line[0] === COMMENT_START; });
        }
        catch (e) {
            if (e.code !== "ENOENT") {
                throw e;
            }
        }
        return rules;
    };
    PathFilteringService.prototype.filterIgnoredFiles = function (files, rules, rootDir) {
        var _this = this;
        return _.reject(files, function (file) { return _this.isFileExcluded(file, rules, rootDir); });
    };
    PathFilteringService.prototype.isFileExcluded = function (file, rules, rootDir) {
        file = file.replace(rootDir, "").replace(new RegExp("^[\\\\|/]*"), "");
        var fileMatched = true;
        _.each(rules, function (rule) {
            var shouldInclude = rule[0] === '!';
            if (shouldInclude) {
                rule = rule.substr(1);
                var ruleMatched = minimatch(file, rule, { nocase: true, dot: true });
                if (ruleMatched) {
                    fileMatched = true;
                }
            }
            else {
                var options = { nocase: true, nonegate: false, dot: true };
                if (rule[0] === '\\' && rule[1] === '!') {
                    rule = rule.substr(1);
                    options.nonegate = true;
                }
                var ruleMatched = minimatch(file, rule, options);
                fileMatched = fileMatched && !ruleMatched;
            }
        });
        return !fileMatched;
    };
    return PathFilteringService;
})();
exports.PathFilteringService = PathFilteringService;
$injector.register("pathFilteringService", PathFilteringService);
