"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

require("core-js/modules/es.array.slice.js");

require("core-js/modules/es.function.name.js");

require("core-js/modules/es.array.from.js");

require("core-js/modules/es.string.iterator.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.symbol.description.js");

require("core-js/modules/es.symbol.iterator.js");

require("core-js/modules/es.array.iterator.js");

require("core-js/modules/web.dom-collections.iterator.js");

require("core-js/modules/es.promise.js");

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.regexp.to-string.js");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FileType = exports.FsReadDirRequest = exports.FsStatRequest = exports.FsContentRequest = void 0;
exports.getRequestService = getRequestService;

var vscode_languageserver_1 = require("vscode-languageserver");

var FsContentRequest;

(function (FsContentRequest) {
  FsContentRequest.type = new vscode_languageserver_1.RequestType('fs/content');
})(FsContentRequest || (exports.FsContentRequest = FsContentRequest = {}));

var FsStatRequest;

(function (FsStatRequest) {
  FsStatRequest.type = new vscode_languageserver_1.RequestType('fs/stat');
})(FsStatRequest || (exports.FsStatRequest = FsStatRequest = {}));

var FsReadDirRequest;

(function (FsReadDirRequest) {
  FsReadDirRequest.type = new vscode_languageserver_1.RequestType('fs/readDir');
})(FsReadDirRequest || (exports.FsReadDirRequest = FsReadDirRequest = {}));

var FileType;

(function (FileType) {
  /**
   * The file type is unknown.
   */
  FileType[FileType["Unknown"] = 0] = "Unknown";
  /**
   * A regular file.
   */

  FileType[FileType["File"] = 1] = "File";
  /**
   * A directory.
   */

  FileType[FileType["Directory"] = 2] = "Directory";
  /**
   * A symbolic link to a file.
   */

  FileType[FileType["SymbolicLink"] = 64] = "SymbolicLink";
})(FileType || (exports.FileType = FileType = {}));

function getRequestService(handledSchemas, connection, runtime) {
  var builtInHandlers = {};

  var _iterator = _createForOfIteratorHelper(handledSchemas),
      _step;

  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var protocol = _step.value;

      if (protocol === 'file') {
        builtInHandlers[protocol] = runtime.file;
      } else if (protocol === 'http' || protocol === 'https') {
        builtInHandlers[protocol] = runtime.http;
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }

  return {
    stat: function stat(uri) {
      return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
        var handler, res;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                handler = builtInHandlers[getScheme(uri)];

                if (!handler) {
                  _context.next = 3;
                  break;
                }

                return _context.abrupt("return", handler.stat(uri));

              case 3:
                _context.next = 5;
                return connection.sendRequest(FsStatRequest.type, uri.toString());

              case 5:
                res = _context.sent;
                return _context.abrupt("return", res);

              case 7:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    readDirectory: function readDirectory(uri) {
      var handler = builtInHandlers[getScheme(uri)];

      if (handler) {
        return handler.readDirectory(uri);
      }

      return connection.sendRequest(FsReadDirRequest.type, uri.toString());
    },
    getContent: function getContent(uri, encoding) {
      var handler = builtInHandlers[getScheme(uri)];

      if (handler) {
        return handler.getContent(uri, encoding);
      }

      return connection.sendRequest(FsContentRequest.type, {
        uri: uri.toString(),
        encoding: encoding
      });
    }
  };
}

function getScheme(uri) {
  return uri.substr(0, uri.indexOf(':'));
}