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';
import { UnknownMacroPlaceholder } from './UnknownMacroPlaceholder';
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
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic extension types; any required for provider compatibility
) {
  const [extKey] = getExtensionKeyAndNodeKey(extensionKey, extensionType);
  return extensionProvider.getExtension(extensionType, extKey);
}
export async function getExtensionModuleNode(extensionProvider, extensionType, extensionKey
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic extension types; any required for provider compatibility
) {
  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 isUnknownConfluenceMacroWithBody(extensionNode) {
  var _extensionNode$parame, _extensionNode$parame2, _extensionNode$parame3;
  return extensionNode !== null && extensionNode.type === 'extension' && extensionNode.extensionType === 'com.atlassian.confluence.macro.core' && !!((_extensionNode$parame = extensionNode.parameters) !== null && _extensionNode$parame !== void 0 && (_extensionNode$parame2 = _extensionNode$parame.macroParams) !== null && _extensionNode$parame2 !== void 0 && (_extensionNode$parame3 = _extensionNode$parame2.__bodyContent) !== null && _extensionNode$parame3 !== void 0 && _extensionNode$parame3.value);
}
function ExtensionLoading(props) {
  const intl = useIntl();
  const extensionNode = props.node;
  if (props.error || props.timedOut) {
    // eslint-disable-next-line no-console
    console.error('Error rendering extension', props.error);
    if (props.error && props.showUnknownMacroPlaceholder && extensionNode && isUnknownConfluenceMacroWithBody(extensionNode) && fg('tinymce_display_unknown_macro_body_content')) {
      return /*#__PURE__*/React.createElement(UnknownMacroPlaceholder, {
        extensionNode: extensionNode
      });
    }
    return /*#__PURE__*/React.createElement("div", null, intl.formatMessage(messages.extensionLoadingError));
  } 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());
      }
    },
    // react-loadable passes all props from <NodeRenderer> to the loading component at runtime,
    // but its TypeScript types only expect LoadingComponentProps. We cast here because
    // ExtensionLoading accepts additional props (node, showUnknownMacroPlaceholder) that
    // react-loadable will pass through but doesn't know about in its type definitions.
    loading: ExtensionLoading
  });
}