import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useMemo } from 'react';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { INPUT_METHOD } from '../../../analytics';
import { WithProviders } from '../../../provider-factory';
import { EditorLinkPicker } from '../EditorLinkPicker';
import HyperlinkAddToolbarComp from './HyperlinkAddToolbar';
const HYPERLINK_PROVIDERS = ['activityProvider', 'searchProvider'];
/**
 * 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: {
    popupWidth: _popupWidth,
    popupHeight: _popupHeight,
    ...linkPickerOptions
  } = {},
  onSubmit,
  displayText,
  displayUrl,
  providerFactory,
  view,
  onCancel,
  invokeMethod,
  lpLinkPicker,
  onClose,
  onEscapeCallback,
  onClickAwayCallback,
  editorAppearance,
  inputMethod,
  searchSessionId,
  timesViewed,
  isOffline
}) {
  const memoizedOnSubmitWithInterface = useMemo(() => onSubmitInterface(onSubmit), [onSubmit]);
  const memoizedRenderNode = useCallback(({
    activityProvider,
    searchProvider
  }) => {
    // If we're offline fallback to HyperlinkAddToolbarComp as we may not have loaded
    // EditorLinkPicker into the bundle
    if (lpLinkPicker && !Boolean(isOffline)) {
      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 : inputMethod,
        editorAppearance: editorAppearance
        // Ignored via go/ees005
        // eslint-disable-next-line react/jsx-props-no-spreading
      }, linkPickerOptions, {
        url: displayUrl,
        displayText: displayText,
        onSubmit: memoizedOnSubmitWithInterface,
        onCancel: onCancel,
        onClose: onClose,
        onEscapeCallback: onEscapeCallback,
        onClickAwayCallback: onClickAwayCallback
      }));
    }
    return /*#__PURE__*/React.createElement(HyperlinkAddToolbarComp, {
      activityProvider: activityProvider,
      searchProvider: searchProvider,
      onSubmit: onSubmit,
      displayText: displayText,
      displayUrl: displayUrl,
      view: view,
      onEscapeCallback: onEscapeCallback,
      onClickAwayCallback: onClickAwayCallback,
      inputMethod: inputMethod,
      searchSessionId: searchSessionId,
      timesViewed: timesViewed
    });
  }, [lpLinkPicker, isOffline, view, invokeMethod, inputMethod, editorAppearance, linkPickerOptions, displayUrl, displayText, onSubmit, memoizedOnSubmitWithInterface, onCancel, onClose, onEscapeCallback, onClickAwayCallback, searchSessionId, timesViewed]);
  const providers = expValEquals('platform_editor_perf_lint_cleanup', 'isEnabled', true) ? HYPERLINK_PROVIDERS :
  // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- intentional fallback for experiment off path
  ['activityProvider', 'searchProvider'];
  return /*#__PURE__*/React.createElement(WithProviders, {
    providers: providers,
    providerFactory: providerFactory,
    renderNode: expValEquals('platform_editor_perf_lint_cleanup', 'isEnabled', true) ? memoizedRenderNode :
    // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- intentional fallback for experiment off path
    ({
      activityProvider,
      searchProvider
    }) => {
      // If we're offline fallback to HyperlinkAddToolbarComp as we may not have loaded
      // EditorLinkPicker into the bundle
      if (lpLinkPicker && !Boolean(isOffline)) {
        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 : inputMethod,
          editorAppearance: editorAppearance
          // Ignored via go/ees005
          // eslint-disable-next-line react/jsx-props-no-spreading
        }, 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,
        view: view,
        onEscapeCallback: onEscapeCallback,
        onClickAwayCallback: onClickAwayCallback,
        inputMethod: inputMethod,
        searchSessionId: searchSessionId,
        timesViewed: timesViewed
      });
    }
  });
}