import { __exports as entry } from '../../../../../../../../_virtual/entry.mjs';
import '../../utils/index.mjs';
import { __exports as utils$1 } from '../../../../../../../../_virtual/index.mjs';

Object.defineProperty(entry, "__esModule", {
  value: true
});
const utils = utils$1;
class EntryFilter {
  constructor(_settings, _micromatchOptions) {
    this._settings = _settings;
    this._micromatchOptions = _micromatchOptions;
    this.index = new Map();
  }
  getFilter(positive, negative) {
    const positiveRe = utils.pattern.convertPatternsToRe(positive, this._micromatchOptions);
    const negativeRe = utils.pattern.convertPatternsToRe(negative, this._micromatchOptions);
    return entry => this._filter(entry, positiveRe, negativeRe);
  }
  _filter(entry, positiveRe, negativeRe) {
    if (this._settings.unique && this._isDuplicateEntry(entry)) {
      return false;
    }
    if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
      return false;
    }
    if (this._isSkippedByAbsoluteNegativePatterns(entry.path, negativeRe)) {
      return false;
    }
    const filepath = this._settings.baseNameMatch ? entry.name : entry.path;
    const isDirectory = entry.dirent.isDirectory();
    const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(entry.path, negativeRe, isDirectory);
    if (this._settings.unique && isMatched) {
      this._createIndexRecord(entry);
    }
    return isMatched;
  }
  _isDuplicateEntry(entry) {
    return this.index.has(entry.path);
  }
  _createIndexRecord(entry) {
    this.index.set(entry.path, undefined);
  }
  _onlyFileFilter(entry) {
    return this._settings.onlyFiles && !entry.dirent.isFile();
  }
  _onlyDirectoryFilter(entry) {
    return this._settings.onlyDirectories && !entry.dirent.isDirectory();
  }
  _isSkippedByAbsoluteNegativePatterns(entryPath, patternsRe) {
    if (!this._settings.absolute) {
      return false;
    }
    const fullpath = utils.path.makeAbsolute(this._settings.cwd, entryPath);
    return utils.pattern.matchAny(fullpath, patternsRe);
  }
  _isMatchToPatterns(entryPath, patternsRe, isDirectory) {
    const filepath = utils.path.removeLeadingDotSegment(entryPath);
    // Trying to match files and directories by patterns.
    const isMatched = utils.pattern.matchAny(filepath, patternsRe);
    // A pattern with a trailling slash can be used for directory matching.
    // To apply such pattern, we need to add a tralling slash to the path.
    if (!isMatched && isDirectory) {
      return utils.pattern.matchAny(filepath + '/', patternsRe);
    }
    return isMatched;
  }
}
entry.default = EntryFilter;
