import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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 \"".concat(extKey, "\" and type \"").concat(extensionType, "\" not found!"));
  }
  if (!manifest.modules.nodes) {
    throw new Error("Couldn't find any node for extension type \"".concat(extensionType, "\" and key \"").concat(extensionKey, "\"!"));
  }
  var node = manifest.modules.nodes[nodeKey];
  if (!node) {
    throw new Error("Node with key \"".concat(extensionKey, "\" not found on manifest for extension type \"").concat(extensionType, "\" and key \"").concat(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
) {
  var _getExtensionKeyAndNo = getExtensionKeyAndNodeKey(extensionKey, extensionType),
    _getExtensionKeyAndNo2 = _slicedToArray(_getExtensionKeyAndNo, 1),
    extKey = _getExtensionKeyAndNo2[0];
  return extensionProvider.getExtension(extensionType, extKey);
}
export function getExtensionModuleNode(_x, _x2, _x3) {
  return _getExtensionModuleNode.apply(this, arguments);
}
function _getExtensionModuleNode() {
  _getExtensionModuleNode = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(extensionProvider, extensionType, extensionKey
  // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Generic extension types; any required for provider compatibility
  ) {
    var _getExtensionKeyAndNo5, _getExtensionKeyAndNo6, extKey, nodeKey, manifest;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _getExtensionKeyAndNo5 = getExtensionKeyAndNodeKey(extensionKey, extensionType), _getExtensionKeyAndNo6 = _slicedToArray(_getExtensionKeyAndNo5, 2), extKey = _getExtensionKeyAndNo6[0], nodeKey = _getExtensionKeyAndNo6[1];
          _context.next = 3;
          return extensionProvider.getExtension(extensionType, extKey);
        case 3:
          manifest = _context.sent;
          return _context.abrupt("return", getNodeFromManifest(manifest, extKey, nodeKey, extensionType, extensionKey));
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return _getExtensionModuleNode.apply(this, arguments);
}
export function getExtensionModuleNodeMaybePreloaded(extensionProvider, extensionType, extensionKey
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) {
  var _extensionProvider$ge;
  var _getExtensionKeyAndNo3 = getExtensionKeyAndNodeKey(extensionKey, extensionType),
    _getExtensionKeyAndNo4 = _slicedToArray(_getExtensionKeyAndNo3, 2),
    extKey = _getExtensionKeyAndNo4[0],
    nodeKey = _getExtensionKeyAndNo4[1];
  var manifest = extensionProvider === null || extensionProvider === 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(function (manifest) {
      return getNodeFromManifest(manifest, extKey, nodeKey, extensionType, extensionKey);
    });
  }
}

/**
 * Gets `__` prefixed properties from an extension node module definition
 */
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) {
            // Ignored via go/ees005
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            acc[key] = moduleNode[key];
            return acc;
          }, {}));
        case 4:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return _getExtensionModuleNodePrivateProps.apply(this, arguments);
}
function isUnknownConfluenceMacroWithBody(extensionNode) {
  var _extensionNode$parame;
  return extensionNode !== null && extensionNode.type === 'extension' && extensionNode.extensionType === 'com.atlassian.confluence.macro.core' && !!((_extensionNode$parame = extensionNode.parameters) !== null && _extensionNode$parame !== void 0 && (_extensionNode$parame = _extensionNode$parame.macroParams) !== null && _extensionNode$parame !== void 0 && (_extensionNode$parame = _extensionNode$parame.__bodyContent) !== null && _extensionNode$parame !== void 0 && _extensionNode$parame.value);
}
function ExtensionLoading(props) {
  var intl = useIntl();
  var 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: function loader() {
      var maybePromise = getExtensionModuleNodeMaybePreloaded(extensionProvider, extensionType, extensionKey);
      if (maybePromise instanceof Promise) {
        return maybePromise.then(function (node) {
          return resolveImport(node.render());
        });
      } else {
        var _renderSync, _ref;
        var preloaded = maybePromise === null || maybePromise === 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
  });
}