import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import { INPUT_METHOD } from '../../../analytics';
import { WithProviders } from '../../../provider-factory';
import { EditorLinkPicker } from '../EditorLinkPicker';
import HyperlinkAddToolbarComp from './HyperlinkAddToolbar';
/**
 * Wraps around the editor's onSubmit handler so that the plugin can interface with the link picker
 */
var onSubmitInterface = function onSubmitInterface(onSubmit) {
  return function (_ref, analytic) {
    var url = _ref.url,
      title = _ref.title,
      displayText = _ref.displayText,
      rawUrl = _ref.rawUrl,
      meta = _ref.meta;
    onSubmit(url, title !== null && title !== void 0 ? title : rawUrl, displayText || undefined, meta.inputMethod === 'manual' ? INPUT_METHOD.MANUAL : INPUT_METHOD.TYPEAHEAD, analytic);
  };
};
export function HyperlinkAddToolbar(_ref2) {
  var _ref2$linkPickerOptio = _ref2.linkPickerOptions,
    linkPickerOptions = _ref2$linkPickerOptio === void 0 ? {} : _ref2$linkPickerOptio,
    onSubmit = _ref2.onSubmit,
    displayText = _ref2.displayText,
    displayUrl = _ref2.displayUrl,
    providerFactory = _ref2.providerFactory,
    view = _ref2.view,
    onCancel = _ref2.onCancel,
    invokeMethod = _ref2.invokeMethod,
    lpLinkPicker = _ref2.lpLinkPicker,
    onClose = _ref2.onClose,
    onEscapeCallback = _ref2.onEscapeCallback,
    onClickAwayCallback = _ref2.onClickAwayCallback,
    hyperlinkPluginState = _ref2.hyperlinkPluginState;
  return /*#__PURE__*/React.createElement(WithProviders, {
    providers: ['activityProvider', 'searchProvider'],
    providerFactory: providerFactory,
    renderNode: function renderNode(_ref3) {
      var activityProvider = _ref3.activityProvider,
        searchProvider = _ref3.searchProvider;
      if (lpLinkPicker) {
        return /*#__PURE__*/React.createElement(EditorLinkPicker, _extends({
          view: view,
          invokeMethod: // Provide `invokeMethod` prop as preferred value (card plugin passes as prop) otherwise assume this
          // is being used from inside the hyperlink plugin and use inputMethod from plugin state
          invokeMethod !== null && invokeMethod !== void 0 ? invokeMethod : hyperlinkPluginState === null || hyperlinkPluginState === void 0 ? void 0 : hyperlinkPluginState.inputMethod,
          editorAppearance: hyperlinkPluginState === null || hyperlinkPluginState === void 0 ? void 0 : hyperlinkPluginState.editorAppearance
        }, linkPickerOptions, {
          url: displayUrl,
          displayText: displayText,
          onSubmit: onSubmitInterface(onSubmit),
          onCancel: onCancel,
          onClose: onClose,
          onEscapeCallback: onEscapeCallback,
          onClickAwayCallback: onClickAwayCallback
        }));
      }
      return /*#__PURE__*/React.createElement(HyperlinkAddToolbarComp, {
        activityProvider: activityProvider,
        searchProvider: searchProvider,
        onSubmit: onSubmit,
        displayText: displayText,
        displayUrl: displayUrl,
        pluginState: hyperlinkPluginState,
        view: view,
        onEscapeCallback: onEscapeCallback,
        onClickAwayCallback: onClickAwayCallback
      });
    }
  });
}