import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React from 'react';
import Loadable from 'react-loadable';
import { getExtensionKeyAndNodeKey, resolveImport } from './manifest-helpers';
export function getExtensionModuleNode(_x, _x2, _x3) {
  return _getExtensionModuleNode.apply(this, arguments);
}

/**
 * Gets `__` prefixed properties from an extension node module definition
 */
function _getExtensionModuleNode() {
  _getExtensionModuleNode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(extensionProvider, extensionType, extensionKey) {
    var _getExtensionKeyAndNo, _getExtensionKeyAndNo2, extKey, nodeKey, manifest, node;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _getExtensionKeyAndNo = getExtensionKeyAndNodeKey(extensionKey, extensionType), _getExtensionKeyAndNo2 = _slicedToArray(_getExtensionKeyAndNo, 2), extKey = _getExtensionKeyAndNo2[0], nodeKey = _getExtensionKeyAndNo2[1];
          _context.next = 3;
          return extensionProvider.getExtension(extensionType, extKey);
        case 3:
          manifest = _context.sent;
          if (manifest) {
            _context.next = 6;
            break;
          }
          throw new Error("Extension with key \"".concat(extKey, "\" and type \"").concat(extensionType, "\" not found!"));
        case 6:
          if (manifest.modules.nodes) {
            _context.next = 8;
            break;
          }
          throw new Error("Couldn't find any node for extension type \"".concat(extensionType, "\" and key \"").concat(extensionKey, "\"!"));
        case 8:
          node = manifest.modules.nodes[nodeKey];
          if (node) {
            _context.next = 11;
            break;
          }
          throw new Error("Node with key \"".concat(extensionKey, "\" not found on manifest for extension type \"").concat(extensionType, "\" and key \"").concat(extensionKey, "\"!"));
        case 11:
          return _context.abrupt("return", node);
        case 12:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _getExtensionModuleNode.apply(this, arguments);
}
export function getExtensionModuleNodePrivateProps(_x4, _x5, _x6) {
  return _getExtensionModuleNodePrivateProps.apply(this, arguments);
}
function _getExtensionModuleNodePrivateProps() {
  _getExtensionModuleNodePrivateProps = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(extensionProvider, extensionType, extensionKey) {
    var moduleNode;
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 2;
          return getExtensionModuleNode(extensionProvider, extensionType, extensionKey);
        case 2:
          moduleNode = _context2.sent;
          return _context2.abrupt("return", Object.keys(moduleNode).filter(function (key) {
            return key.startsWith('__');
          }).reduce(function (acc, key) {
            acc[key] = moduleNode[key];
            return acc;
          }, {}));
        case 4:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return _getExtensionModuleNodePrivateProps.apply(this, arguments);
}
function ExtensionLoading(props) {
  if (props.error || props.timedOut) {
    // eslint-disable-next-line no-console
    console.error('Error rendering extension', props.error);
    return /*#__PURE__*/React.createElement("div", null, "Error loading the extension!");
  } else {
    return null;
  }
}
export function getNodeRenderer(extensionProvider, extensionType, extensionKey) {
  return Loadable({
    loader: function loader() {
      return getExtensionModuleNode(extensionProvider, extensionType, extensionKey).then(function (node) {
        return resolveImport(node.render());
      });
    },
    loading: ExtensionLoading
  });
}