'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _cucumberTagExpressions = require('cucumber-tag-expressions');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var FEATURE_LINENUM_REGEXP = /^(.*?)((?::[\d]+)+)?$/;
var tagExpressionParser = new _cucumberTagExpressions.TagExpressionParser();

var PickleFilter = function () {
  function PickleFilter(_ref) {
    var featurePaths = _ref.featurePaths,
        names = _ref.names,
        tagExpression = _ref.tagExpression;
    (0, _classCallCheck3.default)(this, PickleFilter);

    this.featureUriToLinesMapping = this.getFeatureUriToLinesMapping(featurePaths || []);
    this.names = names || [];
    if (tagExpression) {
      this.tagExpressionNode = tagExpressionParser.parse(tagExpression || '');
    }
  }

  (0, _createClass3.default)(PickleFilter, [{
    key: 'getFeatureUriToLinesMapping',
    value: function getFeatureUriToLinesMapping(featurePaths) {
      var mapping = {};
      featurePaths.forEach(function (featurePath) {
        var match = FEATURE_LINENUM_REGEXP.exec(featurePath);
        if (match) {
          var uri = _path2.default.resolve(match[1]);
          var linesExpression = match[2];
          if (linesExpression) {
            if (!mapping[uri]) {
              mapping[uri] = [];
            }
            linesExpression.slice(1).split(':').forEach(function (line) {
              mapping[uri].push(parseInt(line));
            });
          }
        }
      });
      return mapping;
    }
  }, {
    key: 'matches',
    value: function matches(_ref2) {
      var pickle = _ref2.pickle,
          uri = _ref2.uri;

      return this.matchesAnyLine({ pickle: pickle, uri: uri }) && this.matchesAnyName(pickle) && this.matchesAllTagExpressions(pickle);
    }
  }, {
    key: 'matchesAnyLine',
    value: function matchesAnyLine(_ref3) {
      var pickle = _ref3.pickle,
          uri = _ref3.uri;

      var lines = this.featureUriToLinesMapping[_path2.default.resolve(uri)];
      if (lines) {
        return _lodash2.default.size(_lodash2.default.intersection(lines, _lodash2.default.map(pickle.locations, 'line'))) > 0;
      }
      return true;
    }
  }, {
    key: 'matchesAnyName',
    value: function matchesAnyName(pickle) {
      if (this.names.length === 0) {
        return true;
      }
      return _lodash2.default.some(this.names, function (name) {
        return pickle.name.match(name);
      });
    }
  }, {
    key: 'matchesAllTagExpressions',
    value: function matchesAllTagExpressions(pickle) {
      if (!this.tagExpressionNode) {
        return true;
      }
      return this.tagExpressionNode.evaluate(_lodash2.default.map(pickle.tags, 'name'));
    }
  }]);
  return PickleFilter;
}();

exports.default = PickleFilter;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 5792 characters]