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