import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
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; }
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { combineProviders } from '../provider-helpers';
/**
 * Allow to run methods from the `ExtensionProvider` interface across all providers seamlessly.
 * This handles promise racing and discards rejected promises safely.
 */
export default (function (extensionProviders) {
  var providersCache = [];
  var _combineProviders = combineProviders(extensionProviders),
    invokeSingle = _combineProviders.invokeSingle,
    invokeList = _combineProviders.invokeList;
  return {
    getExtensions: function getExtensions() {
      return invokeList('getExtensions');
    },
    preload: function preload() {
      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function (_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              if (!(providersCache.length === 0)) {
                _context.next = 2;
                break;
              }
              _context.next = 1;
              return Promise.all(extensionProviders.map(function (provider) {
                return Promise.resolve(provider);
              }));
            case 1:
              providersCache = _context.sent;
            case 2:
              _context.next = 3;
              return Promise.all(providersCache.map(function (provider) {
                var _provider$preload;
                return provider === null || provider === void 0 || (_provider$preload = provider.preload) === null || _provider$preload === void 0 ? void 0 : _provider$preload.call(provider);
              }));
            case 3:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }))();
    },
    getPreloadedExtension: function getPreloadedExtension(type, key) {
      if (providersCache.length === 0) {
        // preload() has not been called yet
        return;
      }
      var _iterator = _createForOfIteratorHelper(providersCache),
        _step;
      try {
        for (_iterator.s(); !(_step = _iterator.n()).done;) {
          var provider = _step.value;
          try {
            var _provider$getPreloade;
            var result = provider === null || provider === void 0 || (_provider$getPreloade = provider.getPreloadedExtension) === null || _provider$getPreloade === void 0 ? void 0 : _provider$getPreloade.call(provider, type, key);
            if (result) {
              return result;
            }
          } catch (_unused) {
            // Not every provider will implement this method.
            // In that case we would get error from other providers in the loop
            // and undefined from that particular provider.
            // We can safely ignore it and fallback to the async getExtension when nothing was found.
          }
        }
      } catch (err) {
        _iterator.e(err);
      } finally {
        _iterator.f();
      }
    },
    getExtension: function getExtension(type, key) {
      return invokeSingle('getExtension', [type, key]);
    },
    search: function search(keyword) {
      return invokeList('search', [keyword]);
    },
    getAutoConverter: function getAutoConverter() {
      return invokeList('getAutoConverter');
    }
  };
});