"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
function _anyFs() {
  const data = require("@teambit/any-fs");
  _anyFs = function () {
    return data;
  };
  return data;
}
function eol() {
  const data = _interopRequireWildcard(require("@teambit/toolbox.string.eol"));
  eol = function () {
    return data;
  };
  return data;
}
function _path() {
  const data = _interopRequireDefault(require("path"));
  _path = function () {
    return data;
  };
  return data;
}
function _toolboxPath() {
  const data = require("@teambit/toolbox.path.match-patterns");
  _toolboxPath = function () {
    return data;
  };
  return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * The virtual component filesystem
 */
class ComponentFS extends _anyFs().MemoryFS {
  constructor(
  /**
   * array of all fs files.
   */
  files) {
    super();
    this.files = files;
  }
  /**
   * hash to represent all contents within this filesystem volume.
   */
  get hash() {
    return '';
  }

  /**
   * filter all component files by regex.
   */
  byRegex(extension) {
    return this.files.filter(file => file.path.match(extension));
  }

  /**
   * filter all files using an array of glob patterns.
   */
  byGlob(patterns) {
    const {
      includePatterns,
      excludePatterns
    } = (0, _toolboxPath().splitPatterns)(patterns);
    const files = this.files.filter(file => {
      return (0, _toolboxPath().matchPatterns)(file.relative, includePatterns, excludePatterns);
    });
    return files;
  }
  toObject() {
    return this.files.map(file => {
      return {
        path: file.path,
        contents: file.contents
      };
    });
  }
  static fromVinyls(files) {
    const fs = new ComponentFS(files);
    files.forEach(file => {
      let dirPath = file.relativeDir;
      if (!dirPath.startsWith('/')) dirPath = _path().default.join('/', dirPath);
      fs.mkdirpSync(dirPath);
      fs.writeFileSync(`/${file.relative}`, eol().auto(file.contents || ''));
    });
    return fs;
  }
}
exports.default = ComponentFS;

//# sourceMappingURL=component-fs.js.map