"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _eventemitter = require("eventemitter3");
var _eventType = require("../eventType");
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; }
var Material = exports["default"] = /*#__PURE__*/function (_EventEmitter) {
  (0, _inheritsLoose2["default"])(Material, _EventEmitter);
  function Material(material) {
    var _this;
    _this = _EventEmitter.call(this) || this;
    _this.material = void 0;
    /**
     * 加载组件规格
     * @param infos 
     */
    _this.load = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(infos) {
        var loadedResult, upload;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return _this.material.loadComponentSpec(infos);
            case 2:
              loadedResult = _context.sent;
              upload = infos.filter(function (_, index) {
                return loadedResult[index];
              }).map(function (info) {
                return info.npm;
              });
              if (upload.length) {
                _this.emit(_eventType.ASSET_UPDATED, upload);
              }
              return _context.abrupt("return", loadedResult);
            case 6:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }();
    _this.has = function (packageName) {
      return _this.material.getComponentSpecRawMap().has(packageName);
    };
    _this.get = function (packageName) {
      return _this.getAll().get(packageName);
    };
    _this.getAll = function () {
      var componentSpecRawMap = _this.material.getComponentSpecRawMap();
      var result = new Map();
      for (var _iterator = _createForOfIteratorHelperLoose(componentSpecRawMap), _step; !(_step = _iterator()).done;) {
        var _spec$advanced;
        var _step$value = _step.value,
          key = _step$value[0],
          spec = _step$value[1];
        var component = (_spec$advanced = spec.advanced) === null || _spec$advanced === void 0 ? void 0 : _spec$advanced.component;
        if ((component === null || component === void 0 ? void 0 : component.containerType) !== 'Page') {
          result.set(key, spec);
        }
      }
      return result;
    };
    _this.addComponentSpec = function (packageName, spec) {
      _this.material.addComponentSpec(packageName, spec);
    };
    _this.material = material;
    return _this;
  }
  return Material;
}(_eventemitter.EventEmitter);