import React from 'react';
import Loadable from 'react-loadable';
import { getExtensionKeyAndNodeKey, resolveImport } from './manifest-helpers';
export async function getExtensionModuleNode(extensionProvider, extensionType, extensionKey) {
  const [extKey, nodeKey] = getExtensionKeyAndNodeKey(extensionKey, extensionType);
  const manifest = await extensionProvider.getExtension(extensionType, extKey);
  if (!manifest) {
    throw new Error(`Extension with key "${extKey}" and type "${extensionType}" not found!`);
  }
  if (!manifest.modules.nodes) {
    throw new Error(`Couldn't find any node for extension type "${extensionType}" and key "${extensionKey}"!`);
  }
  const node = manifest.modules.nodes[nodeKey];
  if (!node) {
    throw new Error(`Node with key "${extensionKey}" not found on manifest for extension type "${extensionType}" and key "${extensionKey}"!`);
  }
  return node;
}

/**
 * Gets `__` prefixed properties from an extension node module definition
 */
export async function getExtensionModuleNodePrivateProps(extensionProvider, extensionType, extensionKey) {
  const moduleNode = await getExtensionModuleNode(extensionProvider, extensionType, extensionKey);
  return Object.keys(moduleNode).filter(key => key.startsWith('__')).reduce((acc, key) => {
    acc[key] = moduleNode[key];
    return acc;
  }, {});
}
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: () => {
      return getExtensionModuleNode(extensionProvider, extensionType, extensionKey).then(node => resolveImport(node.render()));
    },
    loading: ExtensionLoading
  });
}