import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["schemaUrl"];
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { useState, useEffect } from 'react';
import { Spin } from 'antd';
import { buildComponents, AssetLoader } from '@alilc/lowcode-utils';
import ReactRenderer from '../../engine-react-renderer/src/index';
import { injectComponents } from '@alilc/lowcode-plugin-inject';
import { getPackagesJson, getProjectSchema } from './utils.js';
import getAppHelper from './appHelper';
import './index.less';
var MaterialsRender = function MaterialsRender(_ref) {
  var schemaUrl = _ref.schemaUrl,
    props = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _useState = useState({}),
    data = _useState[0],
    setData = _useState[1];
  var _useState2 = useState(null),
    packages = _useState2[0],
    setPackages = _useState2[1];
  // const [projectSchema, setProjectSchema] = useState(null);
  useEffect(function () {
    if (!schemaUrl && !props.projectSchema) return;
    init();
  }, [schemaUrl]);
  function init() {
    return _init.apply(this, arguments);
  }
  function _init() {
    _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var projectSchema, _packages, componentsMapArray, componentsTree, componentsMap, schema, libraryMap, libraryAsset, assetLoader, components;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return getProjectSchema({
              url: schemaUrl,
              projectSchema: props.projectSchema
            });
          case 2:
            projectSchema = _context.sent;
            _packages = packages;
            if (_packages) {
              _context.next = 9;
              break;
            }
            _context.next = 7;
            return 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 AssetLoader();
            _context.next = 19;
            return assetLoader.load(libraryAsset);
          case 19:
            _context.next = 21;
            return injectComponents(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.createElement("div", {
      className: "yw-materials-loading"
    }, /*#__PURE__*/React.createElement(Spin, {
      size: "large"
    }));
  }
  // 运行时appHelper 编辑态 props?.appHelper
  var appHelperCombine = (props === null || props === void 0 ? void 0 : props.appHelper) || getAppHelper(props);
  return /*#__PURE__*/React.createElement("div", {
    className: "yw-materials-render"
  }, /*#__PURE__*/React.createElement(ReactRenderer, {
    key: schemaUrl,
    className: "yw-materials-content",
    schema: schema,
    components: components,
    appHelper: appHelperCombine,
    locale: "zh-CN"
  }));
};
export default MaterialsRender;