'use strict';

exports.__esModule = true;
exports.default = ignore;
exports.hasValidExtension = hasValidExtension;

var _path = require('path');

var _es6Set = require('es6-set');

var _es6Set2 = _interopRequireDefault(_es6Set);

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

// one-shot memoized
var cachedSet = void 0,
    lastSettings = void 0;
function validExtensions(_ref) {
  var settings = _ref.settings;

  if (cachedSet && settings === lastSettings) {
    return cachedSet;
  }

  // todo: add 'mjs'?
  lastSettings = settings;
  // breaking: default to '.js'
  // cachedSet = new Set(settings['import/extensions'] || [ '.js' ])
  cachedSet = 'import/extensions' in settings ? makeValidExtensionSet(settings) : { has: function has() {
      return true;
    } }; // the set of all elements

  return cachedSet;
}

function makeValidExtensionSet(settings) {
  // start with explicit JS-parsed extensions
  var exts = new _es6Set2.default(settings['import/extensions']);

  // all alternate parser extensions are also valid
  if ('import/parsers' in settings) {
    for (var parser in settings['import/parsers']) {
      settings['import/parsers'][parser].forEach(function (ext) {
        return exts.add(ext);
      });
    }
  }

  return exts;
}

function ignore(path, context) {
  // ignore node_modules by default
  var ignoreStrings = context.settings['import/ignore'] ? [].concat(context.settings['import/ignore']) : ['node_modules'];

  // check extension list first (cheap)
  if (!hasValidExtension(path, context)) return true;

  if (ignoreStrings.length === 0) return false;

  for (var i = 0; i < ignoreStrings.length; i++) {
    var regex = new RegExp(ignoreStrings[i]);
    if (regex.test(path)) return true;
  }

  return false;
}

function hasValidExtension(path, context) {
  return validExtensions(context).has((0, _path.extname)(path));
}
//# sourceMappingURL=data:application/json;base64,[encoded data, 4324 characters]