import React from 'react';
import { useIntl } from 'react-intl-next';
import Loadable from 'react-loadable';
import { fg } from '@atlaskit/platform-feature-flags';
import { getExtensionKeyAndNodeKey, resolveImport, resolveImportSync } from './manifest-helpers';
import { messages } from './messages';
function getNodeFromManifest(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
manifest, extKey, nodeKey, extensionType, extensionKey) {
  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;
}
export function getExtensionManifest(extensionProvider, extensionType, extensionKey) {
  const [extKey] = getExtensionKeyAndNodeKey(extensionKey, extensionType);
  return extensionProvider.getExtension(extensionType, extKey);
}
export async function getExtensionModuleNode(extensionProvider, extensionType, extensionKey) {
  const [extKey, nodeKey] = getExtensionKeyAndNodeKey(extensionKey, extensionType);
  const manifest = await extensionProvider.getExtension(extensionType, extKey);
  return getNodeFromManifest(manifest, extKey, nodeKey, extensionType, extensionKey);
}
export function getExtensionModuleNodeMaybePreloaded(extensionProvider, extensionType, extensionKey
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) {
  var _extensionProvider$ge;
  const [extKey, nodeKey] = getExtensionKeyAndNodeKey(extensionKey, extensionType);
  const manifest = extensionProvider === null || extensionProvider === void 0 ? void 0 : (_extensionProvider$ge = extensionProvider.getPreloadedExtension) === null || _extensionProvider$ge === void 0 ? void 0 : _extensionProvider$ge.call(extensionProvider, extensionType, extKey);
  if (manifest) {
    return getNodeFromManifest(manifest, extKey, nodeKey, extensionType, extensionKey);
  } else {
    return extensionProvider.getExtension(extensionType, extKey).then(manifest => getNodeFromManifest(manifest, extKey, nodeKey, extensionType, extensionKey));
  }
}

/**
 * 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) => {
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    acc[key] = moduleNode[key];
    return acc;
  }, {});
}
function ExtensionLoading(props) {
  const intl = useIntl();
  if (props.error || props.timedOut) {
    // eslint-disable-next-line no-console
    console.error('Error rendering extension', props.error);
    return /*#__PURE__*/React.createElement("div", null, fg('platform_editor_dec_a11y_fixes') ? intl.formatMessage(messages.extensionLoadingError) : 'Error loading the extension!');
  } else {
    return null;
  }
}
export function getNodeRenderer(extensionProvider, extensionType, extensionKey) {
  return Loadable({
    loader: () => {
      const maybePromise = getExtensionModuleNodeMaybePreloaded(extensionProvider, extensionType, extensionKey);
      if (maybePromise instanceof Promise) {
        return maybePromise.then(node => resolveImport(node.render()));
      } else {
        var _renderSync, _ref;
        const preloaded = maybePromise === null || maybePromise === void 0 ? void 0 : (_renderSync = (_ref = maybePromise).renderSync) === null || _renderSync === void 0 ? void 0 : _renderSync.call(_ref);
        // Only product implemented preloading will return sync result
        // However the out-of-box won't handle this. Confluence uses a custom implementation
        return preloaded ?
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        resolveImportSync(preloaded) : resolveImport(maybePromise.render());
      }
    },
    loading: ExtensionLoading
  });
}