'use strict';

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

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _bitId;

function _load_bitId() {
  return _bitId = _interopRequireDefault(require('../../bit-id/bit-id'));
}

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

function isBitIdMatchByWildcards(bitId, idsWithWildcard) {
  if (!Array.isArray(idsWithWildcard)) idsWithWildcard = [idsWithWildcard];
  var regexPatterns = idsWithWildcard.map(function (id) {
    return getRegex(id);
  });
  var isNameMatchByWildcard = function isNameMatchByWildcard(name) {
    return regexPatterns.some(function (regex) {
      return regex.test(name);
    });
  };
  return isNameMatchByWildcard(bitId.toStringWithoutVersion()) || isNameMatchByWildcard(bitId.toStringWithoutScopeAndVersion());
}

function getRegex(idWithWildcard) {
  if (!(_ramda || _load_ramda()).default.is(String, idWithWildcard)) {
    throw new TypeError(`filterComponentsByWildcard expects idWithWildcard to be string, got ${typeof idWithWildcard}`);
  }
  var rule = idWithWildcard.replace(/\*/g, '.*');
  return new RegExp(`^${rule}$`);
}