import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useEffect, useReducer } from "react";
import { registerPlugins } from "../../../plugins";
import { Spin } from "@whalecloud/fdx";
var globalState = {
  render: false,
  editor: false
};

var handleAfterRegister = function handleAfterRegister(afterRegister) {
  if (afterRegister && typeof afterRegister === 'function') {
    afterRegister();
  }
};

var handleExtraPlugins = function handleExtraPlugins() {
  var extraPlugins = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var plugins = arguments.length > 1 ? arguments[1] : undefined;

  if (extraPlugins.length > 0) {
    extraPlugins.forEach(function (v) {
      if (typeof v === 'function') {
        plugins.push(v());
      } else {
        plugins.push(v);
      }
    });
  }

  return plugins;
};

export function EditorPluginsLoader(_ref) {
  var children = _ref.children,
      type = _ref.type,
      extraPlugins = _ref.extraPlugins,
      afterRegister = _ref.afterRegister;

  var _useReducer = useReducer(function (state, newState) {
    return _objectSpread(_objectSpread({}, state), newState);
  }, globalState),
      _useReducer2 = _slicedToArray(_useReducer, 2),
      loaded = _useReducer2[0],
      setLoaded = _useReducer2[1];

  function loadPlugins() {
    return _loadPlugins.apply(this, arguments);
  }

  function _loadPlugins() {
    _loadPlugins = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var renderPlugins, plugins, _plugins;

      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(type === 'render' && !loaded.render)) {
                _context.next = 10;
                break;
              }

              _context.next = 3;
              return import("../../../render/presets/default");

            case 3:
              renderPlugins = _context.sent;
              plugins = renderPlugins.default;
              handleExtraPlugins(extraPlugins, plugins);
              registerPlugins(plugins); // 是否需要注册自定义插件

              handleAfterRegister(afterRegister);
              globalState.render = true;
              setLoaded({
                render: true
              });

            case 10:
              if (!(type === 'edit' && !loaded.editor)) {
                _context.next = 19;
                break;
              }

              _context.next = 13;
              return Promise.all([import("../../presets/default"), !loaded.render ? import("../../../render/presets/default") : null].filter(Boolean));

            case 13:
              _plugins = _context.sent;
              handleExtraPlugins(extraPlugins, _plugins);
              registerPlugins(_plugins.map(function (p) {
                return p.default || p;
              })); // 是否需要注册自定义插件

              handleAfterRegister(afterRegister);
              globalState.editor = true;
              setLoaded({
                editor: true
              });

            case 19:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));
    return _loadPlugins.apply(this, arguments);
  }

  useEffect(function () {
    loadPlugins();
  }, [loaded]);

  if (loaded.editor && type === 'edit') {
    return children;
  }

  if (loaded.render && type === 'render') {
    return children;
  }

  return /*#__PURE__*/React.createElement(Spin, null);
}
//# sourceMappingURL=PluginsLoader.js.map