"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isDependency = isDependency;
exports.isRelatedPackage = exports.isRelated = void 0;
exports.testPattern = testPattern;

var _path = require("path");

// in our development we only use windows, mac and linux
const isWindows = _path.sep !== '/'; // this function  will  return true if pattern matched

function _testPattern(req, pattern) {
  let modifiedPattern = pattern;

  if (/[*.$^]/.test(modifiedPattern)) {
    if (isWindows) {
      // modifiedPattern = pattern.replace(/\//g, ps.replace(/\\/g, '\\\\'));
      modifiedPattern = modifiedPattern.replace(/\//g, '\\\\');
    }

    modifiedPattern = modifiedPattern.replace(/\./g, '\\.').replace(/\*/g, '.*');
    const re = new RegExp(modifiedPattern);
    return re.test(req);
  }

  if (isWindows) {
    // modifiedPattern = pattern.replace(/\//g, ps.replace(/\\/g, '\\\\'));
    modifiedPattern = modifiedPattern.replace(/\//g, '\\');
  }

  return req.indexOf(modifiedPattern) !== -1;
}

function testPattern(req, pattern) {
  if (!req || !pattern) {
    return false;
  }

  if (Array.isArray(pattern)) {
    // eslint-disable-next-line no-unused-vars
    return pattern.every(p => testPattern(req, p));
  }

  if (pattern[0] === '!') {
    return !_testPattern(req, pattern.slice(1));
  }

  return _testPattern(req, pattern);
}

const isRelated = (req, item) => req && req.indexOf(item) !== -1; // export const isRelated = (req, item) => testPattern(req, item);


exports.isRelated = isRelated;

const isRelatedPackage = (req, item) => isRelated(req, `node_modules${_path.sep}${item}${_path.sep}`) !== -1; // export const isRelated = (req, item) => testPattern(req, item);


exports.isRelatedPackage = isRelatedPackage;

function isDependency(m, excludeList) {
  return m.reasons.some(r => excludeList.some(item => r.module && isRelated(r.module.userRequest, item)));
} // export function queryHandler(conditions, pattern) {
// }