import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["popupWidth", "popupHeight"];
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';
var HYPERLINK_PROVIDERS = ['activityProvider', 'searchProvider'];
/**
 * 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,
    _ref2$linkPickerOptio2 = _ref2$linkPickerOptio === void 0 ? {} : _ref2$linkPickerOptio,
    _popupWidth = _ref2$linkPickerOptio2.popupWidth,
    _popupHeight = _ref2$linkPickerOptio2.popupHeight,
    linkPickerOptions = _objectWithoutProperties(_ref2$linkPickerOptio2, _excluded),
    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,
    editorAppearance = _ref2.editorAppearance,
    inputMethod = _ref2.inputMethod,
    searchSessionId = _ref2.searchSessionId,
    timesViewed = _ref2.timesViewed,
    isOffline = _ref2.isOffline;
  var memoizedOnSubmitWithInterface = useMemo(function () {
    return onSubmitInterface(onSubmit);
  }, [onSubmit]);
  var memoizedRenderNode = useCallback(function (_ref3) {
    var activityProvider = _ref3.activityProvider,
      searchProvider = _ref3.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]);
  var 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
    function (_ref4) {
      var activityProvider = _ref4.activityProvider,
        searchProvider = _ref4.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
      });
    }
  });
}