"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.HyperlinkAddToolbar = HyperlinkAddToolbar;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _analytics = require("../../../analytics");
var _providerFactory = require("../../../provider-factory");
var _EditorLinkPicker = require("../EditorLinkPicker");
var _HyperlinkAddToolbar = _interopRequireDefault(require("./HyperlinkAddToolbar"));
var _excluded = ["popupWidth", "popupHeight"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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' ? _analytics.INPUT_METHOD.MANUAL : _analytics.INPUT_METHOD.TYPEAHEAD, analytic, meta.appearance);
  };
};
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 = (0, _objectWithoutProperties2.default)(_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 = (0, _react.useMemo)(function () {
    return onSubmitInterface(onSubmit);
  }, [onSubmit]);
  var memoizedRenderNode = (0, _react.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.default.createElement(_EditorLinkPicker.EditorLinkPicker, (0, _extends2.default)({
        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.default.createElement(_HyperlinkAddToolbar.default, {
      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 = (0, _expValEquals.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.default.createElement(_providerFactory.WithProviders, {
    providers: providers,
    providerFactory: providerFactory,
    renderNode: (0, _expValEquals.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.default.createElement(_EditorLinkPicker.EditorLinkPicker, (0, _extends2.default)({
          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.default.createElement(_HyperlinkAddToolbar.default, {
        activityProvider: activityProvider,
        searchProvider: searchProvider,
        onSubmit: onSubmit,
        displayText: displayText,
        displayUrl: displayUrl,
        view: view,
        onEscapeCallback: onEscapeCallback,
        onClickAwayCallback: onClickAwayCallback,
        inputMethod: inputMethod,
        searchSessionId: searchSessionId,
        timesViewed: timesViewed
      });
    }
  });
}