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
 */
const onSubmitInterface = onSubmit => ({
  url,
  title,
  displayText,
  rawUrl,
  meta
}, analytic) => {
  onSubmit(url, title !== null && title !== void 0 ? title : rawUrl, displayText || undefined, meta.inputMethod === 'manual' ? INPUT_METHOD.MANUAL : INPUT_METHOD.TYPEAHEAD, analytic);
};
export function HyperlinkAddToolbar({
  linkPickerOptions = {},
  onSubmit,
  displayText,
  displayUrl,
  providerFactory,
  view,
  onCancel,
  invokeMethod,
  lpLinkPicker,
  onClose,
  onEscapeCallback,
  onClickAwayCallback,
  hyperlinkPluginState
}) {
  return /*#__PURE__*/React.createElement(WithProviders, {
    providers: ['activityProvider', 'searchProvider'],
    providerFactory: providerFactory,
    renderNode: ({
      activityProvider,
      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
      });
    }
  });
}