import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
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; }
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { getComponentSpecUrl } from '../utils';
var Material = /*#__PURE__*/function () {
  function Material() {
    this.componentSpecRawMap = new Map();
  }
  var _proto = Material.prototype;
  _proto.loadComponentSpec = /*#__PURE__*/function () {
    var _loadComponentSpec = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(infos) {
      var _this = this;
      var promiseSettledResult, result, _iterator, _step, item, spec;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return Promise.allSettled(infos.map( /*#__PURE__*/function () {
              var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(info) {
                var response;
                return _regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) switch (_context.prev = _context.next) {
                    case 0:
                      if (_this.componentSpecRawMap.has(info.npm)) {
                        console.warn("\u5DF2\u7ECF\u52A0\u8F7D\u8FC7" + info.npm + "\uFF0C\u73B0\u5728\u5F00\u59CB\u91CD\u65B0\u52A0\u8F7D\u5E76\u66FF\u6362\u539F\u6765\u7684");
                      }
                      _context.next = 3;
                      return fetch(getComponentSpecUrl(info));
                    case 3:
                      response = _context.sent;
                      _context.t0 = info;
                      _context.next = 7;
                      return response.text();
                    case 7:
                      _context.t1 = _context.sent;
                      return _context.abrupt("return", {
                        info: _context.t0,
                        text: _context.t1
                      });
                    case 9:
                    case "end":
                      return _context.stop();
                  }
                }, _callee);
              }));
              return function (_x2) {
                return _ref.apply(this, arguments);
              };
            }()));
          case 2:
            promiseSettledResult = _context2.sent;
            result = [];
            for (_iterator = _createForOfIteratorHelperLoose(promiseSettledResult); !(_step = _iterator()).done;) {
              item = _step.value;
              if (item.status === 'rejected') {
                result.push(false);
              } else {
                try {
                  spec = JSON.parse(item.value.text);
                  this.componentSpecRawMap.set(item.value.info.npm, spec);
                  result.push(true);
                } catch (error) {
                  result.push(false);
                }
              }
            }
            return _context2.abrupt("return", result);
          case 6:
          case "end":
            return _context2.stop();
        }
      }, _callee2, this);
    }));
    function loadComponentSpec(_x) {
      return _loadComponentSpec.apply(this, arguments);
    }
    return loadComponentSpec;
  }();
  _proto.getComponentSpecRawMap = function getComponentSpecRawMap() {
    return this.componentSpecRawMap;
  };
  _proto.getComponentSpecRaw = function getComponentSpecRaw(name) {
    return this.componentSpecRawMap.get(name);
  };
  _proto.addComponentSpec = function addComponentSpec(packageName, spec) {
    if (this.componentSpecRawMap.has(packageName)) {
      console.warn(packageName + "\u5DF2\u7ECF\u5B58\u5728\uFF0C\u73B0\u5728\u5F00\u59CB\u91CD\u65B0\u8BBE\u7F6E\u5E76\u66FF\u6362\u539F\u6765\u7684");
    }
    this.componentSpecRawMap.set(packageName, spec);
  };
  return Material;
}();
export { Material as default };