"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _moduleHelpers = require("./module-helpers");
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var DefaultExtensionProvider = exports.default = /*#__PURE__*/function () {
  function DefaultExtensionProvider(manifests,
  /**
   * Allows for an optional list of pre compiled auto convert handlers to be passed.
   * Useful for performance improvements or to support legacy converters.
   *
   * Warning: If this attribute is passed, this provider will ignore auto convert patterns from the manifests.
   */
  autoConvertHandlers) {
    (0, _classCallCheck2.default)(this, DefaultExtensionProvider);
    (0, _defineProperty2.default)(this, "manifestsCache", []);
    this.manifestsPromise = Promise.resolve(manifests);
    this.autoConvertHandlers = autoConvertHandlers;
  }
  return (0, _createClass2.default)(DefaultExtensionProvider, [{
    key: "getExtensions",
    value: function getExtensions() {
      return this.manifestsPromise;
    }
  }, {
    key: "preload",
    value: function () {
      var _preload = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var preloadCalls, _iterator, _step, _manifest$modules, manifest, nodes;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return this.getExtensions();
            case 2:
              this.manifestsCache = _context.sent;
              preloadCalls = [];
              _iterator = _createForOfIteratorHelper(this.manifestsCache);
              try {
                for (_iterator.s(); !(_step = _iterator.n()).done;) {
                  manifest = _step.value;
                  nodes = manifest === null || manifest === void 0 || (_manifest$modules = manifest.modules) === null || _manifest$modules === void 0 ? void 0 : _manifest$modules.nodes;
                  if (nodes) {
                    Object.values(nodes).forEach(function (node) {
                      var _preloadRender, _ref;
                      preloadCalls.push(node === null || node === void 0 || (_preloadRender = (_ref = node).preloadRender) === null || _preloadRender === void 0 ? void 0 : _preloadRender.call(_ref));
                    });
                  }
                }
              } catch (err) {
                _iterator.e(err);
              } finally {
                _iterator.f();
              }
              _context.next = 8;
              return Promise.allSettled(preloadCalls);
            case 8:
            case "end":
              return _context.stop();
          }
        }, _callee, this);
      }));
      function preload() {
        return _preload.apply(this, arguments);
      }
      return preload;
    }()
  }, {
    key: "getPreloadedExtension",
    value: function getPreloadedExtension(type, key) {
      if (!this.manifestsCache) {
        return;
      }
      return this.getExtensionFromManifest(this.manifestsCache, type, key);
    }
  }, {
    key: "getExtension",
    value: function () {
      var _getExtension = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(type, key) {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              _context2.t0 = this;
              _context2.next = 3;
              return this.manifestsPromise;
            case 3:
              _context2.t1 = _context2.sent;
              _context2.t2 = type;
              _context2.t3 = key;
              return _context2.abrupt("return", _context2.t0.getExtensionFromManifest.call(_context2.t0, _context2.t1, _context2.t2, _context2.t3));
            case 7:
            case "end":
              return _context2.stop();
          }
        }, _callee2, this);
      }));
      function getExtension(_x, _x2) {
        return _getExtension.apply(this, arguments);
      }
      return getExtension;
    }()
  }, {
    key: "getExtensionFromManifest",
    value: function getExtensionFromManifest(manifests, type, key) {
      var extension = manifests.find(function (manifest) {
        return manifest.type === type && manifest.key === key;
      });
      if (!extension) {
        throw new Error("Extension with type \"".concat(type, "\" and key \"").concat(key, "\" not found!"));
      }
      return extension;
    }
  }, {
    key: "search",
    value: function () {
      var _search = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(keyword) {
        var extensions;
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return this.manifestsPromise;
            case 2:
              extensions = _context3.sent.filter(function (manifest) {
                return manifest.title.toLowerCase().includes(keyword.toLowerCase());
              });
              return _context3.abrupt("return", extensions);
            case 4:
            case "end":
              return _context3.stop();
          }
        }, _callee3, this);
      }));
      function search(_x3) {
        return _search.apply(this, arguments);
      }
      return search;
    }()
  }, {
    key: "getAutoConverter",
    value: function () {
      var _getAutoConverter = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
        var autoConverters;
        return _regenerator.default.wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              if (!this.autoConvertHandlers) {
                _context4.next = 2;
                break;
              }
              return _context4.abrupt("return", this.autoConvertHandlers);
            case 2:
              _context4.t0 = _moduleHelpers.getAutoConvertPatternsFromModule;
              _context4.next = 5;
              return this.manifestsPromise;
            case 5:
              _context4.t1 = _context4.sent;
              autoConverters = (0, _context4.t0)(_context4.t1);
              return _context4.abrupt("return", autoConverters);
            case 8:
            case "end":
              return _context4.stop();
          }
        }, _callee4, this);
      }));
      function getAutoConverter() {
        return _getAutoConverter.apply(this, arguments);
      }
      return getAutoConverter;
    }()
  }]);
}();