"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 _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var _antd = require("antd");
var _lowcodeUtils = require("@alilc/lowcode-utils");
var _index = _interopRequireDefault(require("../../engine-react-renderer/src/index"));
var _lowcodePluginInject = require("@alilc/lowcode-plugin-inject");
var _utils = require("./utils.js");
var _appHelper = _interopRequireDefault(require("./appHelper"));
require("./index.less");
var _excluded = ["schemaUrl"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var MaterialsRender = function MaterialsRender(_ref) {
  var schemaUrl = _ref.schemaUrl,
    props = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _useState = (0, _react.useState)({}),
    data = _useState[0],
    setData = _useState[1];
  var _useState2 = (0, _react.useState)(null),
    packages = _useState2[0],
    setPackages = _useState2[1];
  // const [projectSchema, setProjectSchema] = useState(null);
  (0, _react.useEffect)(function () {
    if (!schemaUrl && !props.projectSchema) return;
    init();
  }, [schemaUrl]);
  function init() {
    return _init.apply(this, arguments);
  }
  function _init() {
    _init = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var projectSchema, _packages, componentsMapArray, componentsTree, componentsMap, schema, libraryMap, libraryAsset, assetLoader, components;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, _utils.getProjectSchema)({
              url: schemaUrl,
              projectSchema: props.projectSchema
            });
          case 2:
            projectSchema = _context.sent;
            _packages = packages;
            if (_packages) {
              _context.next = 9;
              break;
            }
            _context.next = 7;
            return (0, _utils.getPackagesJson)({
              packages: props.packages
            });
          case 7:
            _packages = _context.sent;
            setPackages(_packages);
          case 9:
            componentsMapArray = projectSchema.componentsMap, componentsTree = projectSchema.componentsTree;
            componentsMap = {};
            componentsMapArray === null || componentsMapArray === void 0 ? void 0 : componentsMapArray.forEach(function (component) {
              componentsMap[component.componentName] = component;
            });
            schema = componentsTree[0];
            libraryMap = {};
            libraryAsset = [];
            _packages.forEach(function (_ref2) {
              var _package = _ref2["package"],
                library = _ref2.library,
                urls = _ref2.urls,
                renderUrls = _ref2.renderUrls;
              libraryMap[_package] = library;
              if (renderUrls) {
                libraryAsset.push(renderUrls);
              } else if (urls) {
                libraryAsset.push(urls);
              }
            });

            // const vendors = [assetBundle(libraryAsset, AssetLevel.Library)];
            assetLoader = new _lowcodeUtils.AssetLoader();
            _context.next = 19;
            return assetLoader.load(libraryAsset);
          case 19:
            _context.next = 21;
            return (0, _lowcodePluginInject.injectComponents)((0, _lowcodeUtils.buildComponents)(libraryMap, componentsMap));
          case 21:
            components = _context.sent;
            setData({
              schema: schema,
              components: components
            });
          case 23:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return _init.apply(this, arguments);
  }
  var schema = data.schema,
    components = data.components;
  if (!schema || !components) {
    // init();
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "yw-materials-loading"
    }, /*#__PURE__*/_react["default"].createElement(_antd.Spin, {
      size: "large"
    }));
  }
  // 运行时appHelper 编辑态 props?.appHelper
  var appHelperCombine = (props === null || props === void 0 ? void 0 : props.appHelper) || (0, _appHelper["default"])(props);
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "yw-materials-render"
  }, /*#__PURE__*/_react["default"].createElement(_index["default"], {
    key: schemaUrl,
    className: "yw-materials-content",
    schema: schema,
    components: components,
    appHelper: appHelperCombine,
    locale: "zh-CN"
  }));
};
var _default = exports["default"] = MaterialsRender;