"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = _interopRequireDefault(require("../../bit-id/bit-id"));

  _bitId = function () {
    return data;
  };

  return data;
}

function isBitIdMatchByWildcards(bitId, idsWithWildcard) {
  if (!Array.isArray(idsWithWildcard)) idsWithWildcard = [idsWithWildcard];
  const regexPatterns = idsWithWildcard.map(id => getRegex(id));

  const isNameMatchByWildcard = name => {
    return regexPatterns.some(regex => regex.test(name));
  };

  return isNameMatchByWildcard(bitId.toStringWithoutVersion()) || isNameMatchByWildcard(bitId.toStringWithoutScopeAndVersion());
}

function getRegex(idWithWildcard) {
  if (!_ramda().default.is(String, idWithWildcard)) {
    throw new TypeError(`filterComponentsByWildcard expects idWithWildcard to be string, got ${typeof idWithWildcard}`);
  }

  const rule = idWithWildcard.replace(/\*/g, '.*');
  return new RegExp(`^${rule}$`);
}