text: "use strict";
  var __generator = (this && this.__generator)/* istanbul ignore next */ || function (thisArg, body) {
      var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
      return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
      function verb(n) { return function (v) { return step([n, v]); }; }
      function step(op) {
          if (f) throw new TypeError("Generator is already executing.");
          while (_) try {
              if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
              if (y = 0, t) op = [0, t.value];
              switch (op[0]) {
                  case 0: case 1: t = op; break;
                  case 4: _.label++; return { value: op[1], done: false };
                  case 5: _.label++; y = op[1]; op = [0]; continue;
                  case 7: op = _.ops.pop(); _.trys.pop(); continue;
                  default:
                      if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                      if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                      if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                      if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                      if (t[2]) _.ops.pop();
                      _.trys.pop(); continue;
              }
              op = body.call(thisArg, _);
          } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
          if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
      }
  };
  var __values = (this && this.__values)/* istanbul ignore next */ || function (o) {
      var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
      if (m) return m.call(o);
      return {
          next: function () {
              if (o && i >= o.length) o = void 0;
              return { value: o && o[i++], done: !o };
          }
      };
  };
  var __read = (this && this.__read)/* istanbul ignore next */ || function (o, n) {
      var m = typeof Symbol === "function" && o[Symbol.iterator];
      if (!m) return o;
      var i = m.call(o), r, ar = [], e;
      try {
          while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
      }
      catch (error) { e = { error: error }; }
      finally {
          try {
              if (r && !r.done && (m = i["return"])) m.call(i);
          }
          finally { if (e) throw e.error; }
      }
      return ar;
  };
  var __spread = (this && this.__spread)/* istanbul ignore next */ || function () {
      for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
      return ar;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  var errors = require("../errors");
  var utils_1 = require("../utils");
  var VirtualFileSystemHost = /** @class */ (function () {
      function VirtualFileSystemHost() {
          this.directories = new utils_1.KeyValueCache();
          this.getOrCreateDir("/");
      }
      VirtualFileSystemHost.prototype.delete = function (path) {
          try {
              this.deleteSync(path);
              return Promise.resolve();
          }
          catch (err) {
              return Promise.reject(err);
          }
      };
      VirtualFileSystemHost.prototype.deleteSync = function (path) {
          path = utils_1.FileUtils.getStandardizedAbsolutePath(this, path);
          if (this.directories.has(path)) {
              try {
                  // remove descendant dirs
                  for (var _a = __values(utils_1.ArrayUtils.from(this.directories.getKeys())), _b = _a.next(); !_b.done; _b = _a.next()) {
                      var directoryPath = _b.value;
                      if (utils_1.StringUtils.startsWith(directoryPath, path))
                          this.directories.removeByKey(directoryPath);
                  }
              }
              catch (e_1_1) { e_1 = { error: e_1_1 }; }
              finally {
                  try {
                      if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
                  }
                  finally { if (e_1) throw e_1.error; }
              }
              // remove this dir
              this.directories.removeByKey(path);
              return;
          }
          var parentDir = this.directories.get(utils_1.FileUtils.getDirPath(path));
          if (parentDir == null || !parentDir.files.has(path))
              throw new errors.FileNotFoundError(path);
          parentDir.files.removeByKey(path);
          var e_1, _c;
      };
      VirtualFileSystemHost.prototype.readDirSync = function (dirPath) {
          dirPath = utils_1.FileUtils.getStandardizedAbsolutePath(this, dirPath);
          var dir = this.directories.get(dirPath);
          if (dir == null)
              throw new errors.DirectoryNotFoundError(dirPath);
          return __spread(getDirectories(this.directories.getKeys()), dir.files.getKeys());
          function getDirectories(dirPaths) {
              var dirPaths_1, dirPaths_1_1, path, parentDir, e_2_1, e_2, _a;
              return __generator(this, function (_b) {
                  switch (_b.label) {
                      case 0:
                          _b.trys.push([0, 5, 6, 7]);
                          dirPaths_1 = __values(dirPaths), dirPaths_1_1 = dirPaths_1.next();
                          _b.label = 1;
                      case 1:
                          if (!!dirPaths_1_1.done) return [3 /*break*/, 4];
                          path = dirPaths_1_1.value;
                          parentDir = utils_1.FileUtils.getDirPath(path);
                          if (!(parentDir === dirPath && parentDir !== path)) return [3 /*break*/, 3];
                          return [4 /*yield*/, path];
                      case 2:
                          _b.sent();
                          _b.label = 3;
                      case 3:
                          dirPaths_1_1 = dirPaths_1.next();
                          return [3 /*break*/, 1];
                      case 4: return [3 /*break*/, 7];
                      case 5:
                          e_2_1 = _b.sent();
                          e_2 = { error: e_2_1 };
                          return [3 /*break*/, 7];
                      case 6:
                          try {
                              if (dirPaths_1_1 && !dirPaths_1_1.done && (_a = dirPaths_1.return)) _a.call(dirPaths_1);
                          }
                          finally { if (e_2) throw e_2.error; }
                          return [7 /*endfinally*/];
                      case 7: return [2 /*return*/];
                  }
              });
          }
      };
      VirtualFileSystemHost.prototype.readFile = function (filePath, encoding) {
          if (encoding === void 0) { encoding = "utf-8"; }
          try {
              return Promise.resolve(this.readFileSync(filePath, encoding));
          }
          catch (err) {
              return Promise.reject(err);
          }
      };
      VirtualFileSystemHost.prototype.readFileSync = function (filePath, encoding) {
          if (encoding === void 0) { encoding = "utf-8"; }
          filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
          var parentDir = this.directories.get(utils_1.FileUtils.getDirPath(filePath));
          if (parentDir == null)
              throw new errors.FileNotFoundError(filePath);
          var fileText = parentDir.files.get(filePath);
          if (fileText === undefined)
              throw new errors.FileNotFoundError(filePath);
          return fileText;
      };
      VirtualFileSystemHost.prototype.writeFile = function (filePath, fileText) {
          this.writeFileSync(filePath, fileText);
          return Promise.resolve();
      };
      VirtualFileSystemHost.prototype.writeFileSync = function (filePath, fileText) {
          filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
          var dirPath = utils_1.FileUtils.getDirPath(filePath);
          this.getOrCreateDir(dirPath).files.set(filePath, fileText);
      };
      VirtualFileSystemHost.prototype.mkdir = function (dirPath) {
          this.mkdirSync(dirPath);
          return Promise.resolve();
      };
      VirtualFileSystemHost.prototype.mkdirSync = function (dirPath) {
          dirPath = utils_1.FileUtils.getStandardizedAbsolutePath(this, dirPath);
          this.getOrCreateDir(dirPath);
      };
      VirtualFileSystemHost.prototype.fileExists = function (filePath) {
          return Promise.resolve(this.fileExistsSync(filePath));
      };
      VirtualFileSystemHost.prototype.fileExistsSync = function (filePath) {
          filePath = utils_1.FileUtils.getStandardizedAbsolutePath(this, filePath);
          var dirPath = utils_1.FileUtils.getDirPath(filePath);
          var dir = this.directories.get(dirPath);
          if (dir == null)
              return false;
          return dir.files.has(filePath);
      };
      VirtualFileSystemHost.prototype.directoryExists = function (dirPath) {
          return Promise.resolve(this.directoryExistsSync(dirPath));
      };
      VirtualFileSystemHost.prototype.directoryExistsSync = function (dirPath) {
          dirPath = utils_1.FileUtils.getStandardizedAbsolutePath(this, dirPath);
          return this.directories.has(dirPath);
      };
      VirtualFileSystemHost.prototype.getCurrentDirectory = function () {
          return "/";
      };
      VirtualFileSystemHost.prototype.glob = function (patterns) {
          var filePaths = [];
          var allFilePaths = utils_1.ArrayUtils.from(getAllFilePaths(this.directories.getValues()));
          return utils_1.matchGlobs(allFilePaths, patterns, this.getCurrentDirectory());
          function getAllFilePaths(directories) {
              var directories_1, directories_1_1, dir, e_3_1, e_3, _a;
              return __generator(this, function (_b) {
                  switch (_b.label) {
                      case 0:
                          _b.trys.push([0, 5, 6, 7]);
                          directories_1 = __values(directories), directories_1_1 = directories_1.next();
                          _b.label = 1;
                      case 1:
                          if (!!directories_1_1.done) return [3 /*break*/, 4];
                          dir = directories_1_1.value;
                          return [5 /*yield**/, __values(dir.files.getKeys())];
                      case 2:
                          _b.sent();
                          _b.label = 3;
                      case 3:
                          directories_1_1 = directories_1.next();
                          return [3 /*break*/, 1];
                      case 4: return [3 /*break*/, 7];
                      case 5:
                          e_3_1 = _b.sent();
                          e_3 = { error: e_3_1 };
                          return [3 /*break*/, 7];
                      case 6:
                          try {
                              if (directories_1_1 && !directories_1_1.done && (_a = directories_1.return)) _a.call(directories_1);
                          }
                          finally { if (e_3) throw e_3.error; }
                          return [7 /*endfinally*/];
                      case 7: return [2 /*return*/];
                  }
              });
          }
      };
      VirtualFileSystemHost.prototype.getOrCreateDir = function (dirPath) {
          var dir = this.directories.get(dirPath);
          if (dir == null) {
              dir = { path: dirPath, files: new utils_1.KeyValueCache() };
              this.directories.set(dirPath, dir);
              var parentDirPath = utils_1.FileUtils.getDirPath(dirPath);
              if (parentDirPath !== dirPath)
                  this.getOrCreateDir(parentDirPath);
          }
          return dir;
      };
      return VirtualFileSystemHost;
  }());
  exports.VirtualFileSystemHost = VirtualFileSystemHost;
  