"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.visuallyHiddenStyles = exports.messages = exports.default = exports.RECENT_SEARCH_LIST_SIZE = exports.HyperlinkLinkAddToolbarWithIntl = exports.HyperlinkLinkAddToolbar = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _reactDom = require("react-dom");
var _reactFocusLock = _interopRequireDefault(require("react-focus-lock"));
var _reactIntlNext = require("react-intl-next");
var _adfSchema = require("@atlaskit/adf-schema");
var _withAnalyticsEvents = _interopRequireDefault(require("@atlaskit/analytics-next/withAnalyticsEvents"));
var _ = _interopRequireDefault(require("@atlaskit/icon-object/glyph/page/16"));
var _crossCircle = _interopRequireDefault(require("@atlaskit/icon/core/cross-circle"));
var _primitives = require("@atlaskit/primitives");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _tooltip = _interopRequireDefault(require("@atlaskit/tooltip"));
var _analytics = require("../../../analytics");
var _ui = require("../../../ui");
var _utils = require("../../../utils");
var _browser = require("../../../utils/browser");
var _LinkSearchList = _interopRequireDefault(require("../../LinkSearch/LinkSearchList"));
var _ToolbarComponents = require("../../LinkSearch/ToolbarComponents");
var _transformTimeStamp = require("../../LinkSearch/transformTimeStamp");
var _utils2 = require("./utils");
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); }
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /* eslint-disable @atlaskit/ui-styling-standard/use-compiled -- Pre-existing lint debt surfaced by this mechanical type-import-only PR. */ /**
 * @jsxRuntime classic
 * @jsx jsx
 */ // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled, @typescript-eslint/consistent-type-imports -- Ignored via go/DSP-18766; jsx required at runtime for @jsxRuntime classic
// eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss
/**
 * Visible only to screenreaders. Use when there is a need
 * to provide more context to a non-sighted user.
 */
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-exported-styles -- Ignored via go/DSP-18766
var visuallyHiddenStyles = exports.visuallyHiddenStyles = (0, _react2.css)({
  clip: 'rect(1px, 1px, 1px, 1px)',
  clipPath: 'inset(50%)',
  height: '1px',
  width: '1px',
  margin: "var(--ds-space-negative-025, -2px)",
  overflow: 'hidden',
  padding: 0,
  position: 'absolute'
});
var RECENT_SEARCH_LIST_SIZE = exports.RECENT_SEARCH_LIST_SIZE = 5;
var clearTextButtonStyles = (0, _primitives.xcss)({
  padding: 'space.0',
  marginRight: 'space.100',
  backgroundColor: 'color.background.neutral.subtle',
  border: 'none'
});
var clearTextWrapper = (0, _react2.css)({
  position: 'absolute',
  right: 0
});
var containerPadding = (0, _react2.css)({
  padding: "var(--ds-space-150, 12px)".concat(" ", "var(--ds-space-100, 8px)")
});
var textLabelMargin = (0, _react2.css)({
  marginTop: "var(--ds-space-150, 12px)"
});
var inputLabel = (0, _react2.css)({
  color: "var(--ds-text-subtlest, #6B6E76)",
  paddingBottom: "var(--ds-space-050, 4px)",
  font: "var(--ds-font-body-small, normal 400 12px/16px \"Atlassian Sans\", ui-sans-serif, -apple-system, BlinkMacSystemFont, \"Segoe UI\", Ubuntu, \"Helvetica Neue\", sans-serif)",
  fontWeight: "var(--ds-font-weight-medium, 500)"
});
var inputWrapperPosition = (0, _react2.css)({
  position: 'relative'
});
var messages = exports.messages = (0, _reactIntlNext.defineMessages)({
  displayText: {
    id: 'fabric.editor.displayText',
    defaultMessage: 'Text to display',
    description: 'Label for the text input field in the hyperlink toolbar where users enter the display text for a link.'
  },
  clearText: {
    id: 'fabric.editor.clearLinkText',
    defaultMessage: 'Clear text',
    description: 'Clears text on the link toolbar'
  },
  clearLink: {
    id: 'fabric.editor.clearLink',
    defaultMessage: 'Clear link',
    description: 'Clears link in the link toolbar'
  },
  hyperlinkAriaLabel: {
    id: 'fabric.editor.hyperlink.ariaLabel',
    defaultMessage: 'Hyperlink Edit',
    description: 'Aria label for the Hyperlink Add Toolbar'
  },
  searchLinkAriaDescription: {
    id: 'fabric.editor.hyperlink.searchLinkAriaDescription',
    defaultMessage: 'Suggestions will appear below as you type into the field',
    description: 'Describes what the search field does for screen reader users.'
  },
  searchLinkResults: {
    id: 'fabric.editor.hyperlink.searchLinkResults',
    defaultMessage: '{count, plural, =0 {no results} one {# result} other {# results}} found',
    description: 'Announce search results for screen-reader users.'
  },
  linkVisibleLabel: {
    id: 'fabric.editor.hyperlink.linkVisibleLabel',
    defaultMessage: 'Paste or search for link',
    description: 'Visible label for link input in hyperlink floating control'
  },
  textVisibleLabel: {
    id: 'fabric.editor.hyperlink.textVisibleLabel',
    defaultMessage: 'Display text (optional)',
    description: 'Visible label for text input in hyperlink floating control'
  }
});
var defaultIcon = (0, _react2.jsx)(_.default, {
  label: 'page'
});
var mapActivityProviderResultToLinkSearchItemData = function mapActivityProviderResultToLinkSearchItemData(_ref) {
  var name = _ref.name,
    container = _ref.container,
    iconUrl = _ref.iconUrl,
    objectId = _ref.objectId,
    url = _ref.url,
    viewedTimestamp = _ref.viewedTimestamp;
  return {
    objectId: objectId,
    name: name,
    container: container,
    iconUrl: iconUrl,
    url: url,
    lastViewedDate: viewedTimestamp ? new Date(viewedTimestamp) : undefined,
    prefetch: true
  };
};
var mapSearchProviderResultToLinkSearchItemData = function mapSearchProviderResultToLinkSearchItemData(_ref2) {
  var objectId = _ref2.objectId,
    container = _ref2.container,
    title = _ref2.title,
    contentType = _ref2.contentType,
    url = _ref2.url,
    updatedTimestamp = _ref2.updatedTimestamp;
  return {
    objectId: objectId,
    container: container,
    name: title,
    url: url,
    lastUpdatedDate: updatedTimestamp ? new Date(updatedTimestamp) : undefined,
    icon: contentType && _utils2.mapContentTypeToIcon[contentType] || defaultIcon,
    prefetch: false
  };
};

// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components
var HyperlinkLinkAddToolbar = exports.HyperlinkLinkAddToolbar = /*#__PURE__*/function (_PureComponent) {
  function HyperlinkLinkAddToolbar(props) {
    var _this;
    (0, _classCallCheck2.default)(this, HyperlinkLinkAddToolbar);
    _this = _callSuper(this, HyperlinkLinkAddToolbar, [props]);
    /* To prevent double submit */
    (0, _defineProperty2.default)(_this, "submitted", false);
    (0, _defineProperty2.default)(_this, "urlInputContainer", null);
    (0, _defineProperty2.default)(_this, "displayTextInputContainer", null);
    (0, _defineProperty2.default)(_this, "wrapperRef", /*#__PURE__*/_react.default.createRef());
    (0, _defineProperty2.default)(_this, "listItemRefs", {
      current: {}
    });
    (0, _defineProperty2.default)(_this, "quickSearchQueryVersion", 0);
    (0, _defineProperty2.default)(_this, "analyticSource", 'createLinkInlineDialog');
    (0, _defineProperty2.default)(_this, "quickSearch", /*#__PURE__*/function () {
      var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(input, items, quickSearchLimit) {
        var _this$state, searchProvider, displayUrl, searchSessionId, queryVersion, perfStart, searchProviderResultItems, searchItems, perfStop, duration, _perfStop, _duration;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _this$state = _this.state, searchProvider = _this$state.searchProvider, displayUrl = _this$state.displayUrl;
              searchSessionId = _this.props.searchSessionId;
              if (searchProvider) {
                _context.next = 4;
                break;
              }
              return _context.abrupt("return");
            case 4:
              queryVersion = ++_this.quickSearchQueryVersion;
              _this.fireAnalytics({
                action: _analytics.ACTION.ENTERED,
                actionSubject: _analytics.ACTION_SUBJECT.TEXT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.LINK_SEARCH_INPUT,
                attributes: {
                  queryLength: input.length,
                  queryVersion: queryVersion,
                  queryHash: (0, _utils2.sha1)(input),
                  searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
                  wordCount: (0, _utils2.wordCount)(input),
                  source: _this.analyticSource
                },
                nonPrivacySafeAttributes: {
                  query: input
                },
                eventType: _analytics.EVENT_TYPE.UI
              });
              perfStart = performance.now();
              _context.prev = 7;
              _context.next = 10;
              return searchProvider.quickSearch(input, quickSearchLimit);
            case 10:
              searchProviderResultItems = _context.sent;
              searchItems = limit((0, _utils2.filterUniqueItems)([].concat((0, _toConsumableArray2.default)(items), (0, _toConsumableArray2.default)(searchProviderResultItems.map(mapSearchProviderResultToLinkSearchItemData))), function (firstItem, secondItem) {
                return firstItem.objectId === secondItem.objectId;
              }));
              if (displayUrl === input && queryVersion === _this.quickSearchQueryVersion) {
                _this.setState({
                  items: searchItems,
                  isLoading: false
                });
              }
              perfStop = performance.now();
              duration = perfStop - perfStart;
              _this.fireAnalytics({
                action: _analytics.ACTION.INVOKED,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.QUICK_SEARCH,
                attributes: {
                  duration: duration,
                  count: searchProviderResultItems.length
                },
                eventType: _analytics.EVENT_TYPE.OPERATIONAL
              });
              _this.fireAnalytics({
                action: _analytics.ACTION.SHOWN,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.POST_QUERY_SEARCH_RESULTS,
                attributes: {
                  source: _this.analyticSource,
                  postQueryRequestDurationMs: duration,
                  searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
                  resultCount: searchProviderResultItems.length,
                  results: searchProviderResultItems.map(function (item) {
                    return {
                      resultContentId: item.objectId,
                      resultType: item.contentType
                    };
                  })
                },
                eventType: _analytics.EVENT_TYPE.UI
              });
              // Ignored via go/ees005
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
              _context.next = 24;
              break;
            case 19:
              _context.prev = 19;
              _context.t0 = _context["catch"](7);
              _perfStop = performance.now();
              _duration = _perfStop - perfStart;
              _this.fireAnalytics({
                action: _analytics.ACTION.INVOKED,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.QUICK_SEARCH,
                attributes: {
                  duration: _duration,
                  count: -1,
                  errorCode: _context.t0.status
                },
                eventType: _analytics.EVENT_TYPE.OPERATIONAL
              });
            case 24:
            case "end":
              return _context.stop();
          }
        }, _callee, null, [[7, 19]]);
      }));
      return function (_x, _x2, _x3) {
        return _ref3.apply(this, arguments);
      };
    }());
    (0, _defineProperty2.default)(_this, "updateInput", /*#__PURE__*/function () {
      var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(input) {
        var _this$state2, activityProvider, searchProvider, items, shouldQuerySearchProvider;
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) switch (_context2.prev = _context2.next) {
            case 0:
              _this$state2 = _this.state, activityProvider = _this$state2.activityProvider, searchProvider = _this$state2.searchProvider;
              _this.setState({
                displayUrl: input
              });
              if (!activityProvider) {
                _context2.next = 23;
                break;
              }
              if (!(input.length === 0)) {
                _context2.next = 13;
                break;
              }
              _context2.t0 = _this;
              _context2.next = 7;
              return _this.getRecentItems(activityProvider);
            case 7:
              _context2.t1 = _context2.sent;
              _context2.t2 = -1;
              _context2.t3 = {
                items: _context2.t1,
                selectedIndex: _context2.t2
              };
              _context2.t0.setState.call(_context2.t0, _context2.t3);
              _context2.next = 23;
              break;
            case 13:
              if (!(0, _adfSchema.isSafeUrl)(input)) {
                _context2.next = 17;
                break;
              }
              _this.setState({
                items: [],
                selectedIndex: -1,
                isLoading: false
              });
              _context2.next = 23;
              break;
            case 17:
              _context2.next = 19;
              return _this.getRecentItems(activityProvider, input);
            case 19:
              items = _context2.sent;
              shouldQuerySearchProvider = items.length < RECENT_SEARCH_LIST_SIZE && !!searchProvider;
              _this.setState({
                items: items,
                isLoading: shouldQuerySearchProvider
              });
              if (shouldQuerySearchProvider) {
                _this.debouncedQuickSearch(input, items, RECENT_SEARCH_LIST_SIZE);
              }
            case 23:
            case "end":
              return _context2.stop();
          }
        }, _callee2);
      }));
      return function (_x4) {
        return _ref4.apply(this, arguments);
      };
    }());
    (0, _defineProperty2.default)(_this, "createClearHandler", function (field) {
      return /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
        var activityProvider;
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) switch (_context3.prev = _context3.next) {
            case 0:
              activityProvider = _this.state.activityProvider;
              _context3.t0 = field;
              _context3.next = _context3.t0 === 'displayUrl' ? 4 : _context3.t0 === 'displayText' ? 21 : 23;
              break;
            case 4:
              _context3.t1 = _this;
              _context3.t2 = _defineProperty2.default;
              _context3.t3 = (0, _defineProperty2.default)({}, field, '');
              if (!activityProvider) {
                _context3.next = 15;
                break;
              }
              _context3.t5 = limit;
              _context3.next = 11;
              return activityProvider.getRecentItems();
            case 11:
              _context3.t6 = _context3.sent;
              _context3.t4 = (0, _context3.t5)(_context3.t6);
              _context3.next = 16;
              break;
            case 15:
              _context3.t4 = [];
            case 16:
              _context3.t7 = _context3.t4;
              _context3.t8 = (0, _context3.t2)(_context3.t3, "items", _context3.t7);
              _context3.t1.setState.call(_context3.t1, _context3.t8);
              if (_this.urlInputContainer) {
                _this.urlInputContainer.focus();
              }
              return _context3.abrupt("break", 23);
            case 21:
              _this.setState((0, _defineProperty2.default)({}, field, ''));
              if (_this.displayTextInputContainer) {
                _this.displayTextInputContainer.focus();
              }
            case 23:
            case "end":
              return _context3.stop();
          }
        }, _callee3);
      }));
    });
    (0, _defineProperty2.default)(_this, "handleClickOutside", function (event) {
      if (event.target instanceof Element && _this.wrapperRef.current && !_this.wrapperRef.current.contains(event.target)) {
        var _this$props = _this.props,
          _this$props$view = _this$props.view,
          state = _this$props$view.state,
          dispatch = _this$props$view.dispatch,
          onClickAwayCallback = _this$props.onClickAwayCallback;
        onClickAwayCallback === null || onClickAwayCallback === void 0 || onClickAwayCallback(state, dispatch);
      }
    });
    (0, _defineProperty2.default)(_this, "getScreenReaderText", function () {
      var intl = _this.props.intl;
      var _this$state3 = _this.state,
        items = _this$state3.items,
        selectedIndex = _this$state3.selectedIndex;
      if (items.length && selectedIndex > -1) {
        var _items$selectedIndex = items[selectedIndex],
          name = _items$selectedIndex.name,
          _container = _items$selectedIndex.container,
          lastUpdatedDate = _items$selectedIndex.lastUpdatedDate,
          lastViewedDate = _items$selectedIndex.lastViewedDate;
        var date = (0, _transformTimeStamp.transformTimeStamp)(intl, lastViewedDate, lastUpdatedDate);
        return "".concat(name, ", ").concat(_container, ", ").concat(date === null || date === void 0 ? void 0 : date.pageAction, " ").concat(date === null || date === void 0 ? void 0 : date.dateString, " ").concat((date === null || date === void 0 ? void 0 : date.timeSince) || '');
      }
    });
    (0, _defineProperty2.default)(_this, "listItemRefCallback", function (el, id) {
      if (!_this.listItemRefs.current) {
        return;
      }
      if (el === null) {
        delete _this.listItemRefs.current[id];
      } else {
        _this.listItemRefs.current[id] = el;
      }
    });
    (0, _defineProperty2.default)(_this, "isUrlPopulatedWithSelectedItem", function () {
      /**
       * When we use ArrowKey to navigate through result items,
       * the URL field will be populated with the content of
       * selected item.
       * This function will check if the URL field is populated
       * with selected item.
       * It can be useful to detect whether we want to insert a
       * smartlink or a hyperlink with customized title
       */
      var _this$state4 = _this.state,
        items = _this$state4.items,
        selectedIndex = _this$state4.selectedIndex,
        displayUrl = _this$state4.displayUrl;
      var selectedItem = items[selectedIndex];
      if (selectedItem && selectedItem.url === displayUrl) {
        return true;
      }
      return false;
    });
    (0, _defineProperty2.default)(_this, "handleSelected", function (href, text) {
      _this.handleInsert(href, text, _analytics.INPUT_METHOD.TYPEAHEAD, 'click');
    });
    (0, _defineProperty2.default)(_this, "handleInsert", function (href, title, inputType, interaction) {
      var _this$props2 = _this.props,
        searchSessionId = _this$props2.searchSessionId,
        onSubmit = _this$props2.onSubmit;
      var _this$state5 = _this.state,
        items = _this$state5.items,
        selectedIndex = _this$state5.selectedIndex,
        displayText = _this$state5.displayText;
      if (onSubmit) {
        _this.submitted = true;
        onSubmit(href, title, displayText, inputType);
      }
      if (interaction === 'click' || _this.isUrlPopulatedWithSelectedItem()) {
        var _selectedItem$prefetc;
        /**
         * When it's a mouse click even or the selectedItem.url matches displayUrl, we think
         * it's selected from the result list and fire the
         * analytic
         */
        var selectedItem = items[selectedIndex];
        _this.fireAnalytics({
          action: _analytics.ACTION.SELECTED,
          actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
          attributes: {
            source: _this.analyticSource,
            searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
            trigger: interaction,
            resultCount: items.length,
            selectedResultId: selectedItem.objectId,
            selectedRelativePosition: selectedIndex,
            prefetch: (_selectedItem$prefetc = selectedItem.prefetch) !== null && _selectedItem$prefetc !== void 0 ? _selectedItem$prefetc : false
          },
          eventType: _analytics.EVENT_TYPE.UI
        });
      }
    });
    (0, _defineProperty2.default)(_this, "handleMouseEnterResultItem", function (objectId) {
      var items = _this.state.items;
      var index = findIndex(items, function (item) {
        return item.objectId === objectId;
      });
      _this.setState({
        selectedIndex: index
      });
    });
    (0, _defineProperty2.default)(_this, "handleMouseLeaveResultItem", function (objectId) {
      var _this$state6 = _this.state,
        items = _this$state6.items,
        selectedIndex = _this$state6.selectedIndex;
      var index = findIndex(items, function (item) {
        return item.objectId === objectId;
      });
      // This is to avoid updating index that was set by other mouseenter event
      if (selectedIndex === index) {
        _this.setState({
          selectedIndex: -1
        });
      }
    });
    (0, _defineProperty2.default)(_this, "handleSubmit", function () {
      var _this$state7 = _this.state,
        displayUrl = _this$state7.displayUrl,
        selectedIndex = _this$state7.selectedIndex,
        items = _this$state7.items;
      var selectedItem = items[selectedIndex];
      if (_this.isUrlPopulatedWithSelectedItem()) {
        _this.handleInsert((0, _utils.normalizeUrl)(selectedItem.url), selectedItem.name, _analytics.INPUT_METHOD.TYPEAHEAD, 'keyboard');
      } else if (displayUrl && displayUrl.length > 0) {
        var url = (0, _utils.normalizeUrl)(displayUrl);
        if (url) {
          _this.handleInsert(url, displayUrl, _analytics.INPUT_METHOD.MANUAL, 'notselected');
        }
      }
    });
    (0, _defineProperty2.default)(_this, "handleClearTextKeyDown", function (event) {
      var KEY_CODE_TAB = 9;
      var keyCode = event.keyCode;
      if (keyCode === KEY_CODE_TAB) {
        /** If there are items in the list, allow normal tabbing so focus moves to the next interactive element (in this case, a search result item).
         */
        if (_this.state.items.length > 0) {
          return;
        }
        if (!_this.submitted) {
          var _this$state8 = _this.state,
            displayUrl = _this$state8.displayUrl,
            displayText = _this$state8.displayText;
          var url = (0, _utils.normalizeUrl)(displayUrl);
          _this.handleInsert(url, displayText || displayUrl, _analytics.INPUT_METHOD.MANUAL, 'notselected');
        }
        event.preventDefault();
        return;
      }
    });
    (0, _defineProperty2.default)(_this, "handleListItemFocus", function (index) {
      _this.setState({
        selectedIndex: index
      });
    });
    (0, _defineProperty2.default)(_this, "handleListItemBlur", function () {
      _this.setState({
        selectedIndex: -1
      });
    });
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (0, _defineProperty2.default)(_this, "handleKeyDown", function (event) {
      var _this$state9 = _this.state,
        items = _this$state9.items,
        selectedIndex = _this$state9.selectedIndex;
      var _this$props3 = _this.props,
        view = _this$props3.view,
        onEscapeCallback = _this$props3.onEscapeCallback,
        searchSessionId = _this$props3.searchSessionId;
      var keyCode = event.keyCode;
      var KEY_CODE_ESCAPE = 27;
      var KEY_CODE_ARROW_DOWN = 40;
      var KEY_CODE_ARROW_UP = 38;
      if (keyCode === KEY_CODE_ESCAPE) {
        // escape
        event.preventDefault();
        var state = view.state,
          dispatch = view.dispatch;
        onEscapeCallback === null || onEscapeCallback === void 0 || onEscapeCallback(state, dispatch);
        return;
      }
      if (!items || !items.length) {
        return;
      }
      var updatedIndex = selectedIndex;
      if (keyCode === KEY_CODE_ARROW_DOWN) {
        // down
        event.preventDefault();
        updatedIndex = (selectedIndex + 1) % items.length;
      } else if (keyCode === KEY_CODE_ARROW_UP) {
        // up
        event.preventDefault();
        updatedIndex = selectedIndex > 0 ? selectedIndex - 1 : items.length - 1;
      }
      if ([KEY_CODE_ARROW_DOWN, KEY_CODE_ARROW_UP].includes(keyCode) && items[updatedIndex]) {
        if (_this.listItemRefs.current) {
          var _this$listItemRefs$cu;
          (_this$listItemRefs$cu = _this.listItemRefs.current[items[updatedIndex].objectId]) === null || _this$listItemRefs$cu === void 0 || _this$listItemRefs$cu.focus();
        }
        _this.setState({
          selectedIndex: updatedIndex,
          displayUrl: items[updatedIndex].url
        });
        _this.fireAnalytics({
          action: _analytics.ACTION.HIGHLIGHTED,
          actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
          attributes: {
            source: _this.analyticSource,
            searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
            selectedResultId: items[updatedIndex].objectId,
            selectedRelativePosition: updatedIndex
          },
          eventType: _analytics.EVENT_TYPE.UI
        });
      }
    });
    (0, _defineProperty2.default)(_this, "updateTextInput", function (displayText) {
      _this.setState({
        displayText: displayText
      });
    });
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    (0, _defineProperty2.default)(_this, "handleCancel", function (e) {
      var _this$props4 = _this.props,
        _this$props4$view = _this$props4.view,
        state = _this$props4$view.state,
        dispatch = _this$props4$view.dispatch,
        onClickAwayCallback = _this$props4.onClickAwayCallback;
      e.preventDefault();
      onClickAwayCallback === null || onClickAwayCallback === void 0 || onClickAwayCallback(state, dispatch);
    });
    _this.state = {
      selectedIndex: -1,
      isLoading: false,
      displayUrl: (0, _utils.normalizeUrl)(props.displayUrl),
      displayText: props.displayText,
      items: []
    };

    /* Cache functions */
    _this.handleClearText = _this.createClearHandler('displayUrl');
    _this.handleClearDisplayText = _this.createClearHandler('displayText');
    _this.debouncedQuickSearch = (0, _debounce.default)(_this.quickSearch, 400);
    _this.fireCustomAnalytics = (0, _analytics.fireAnalyticsEvent)(props.createAnalyticsEvent);
    return _this;
  }
  (0, _inherits2.default)(HyperlinkLinkAddToolbar, _PureComponent);
  return (0, _createClass2.default)(HyperlinkLinkAddToolbar, [{
    key: "componentDidMount",
    value: function () {
      var _componentDidMount = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
        var _this2 = this;
        var _this$props5, timesViewed, inputMethod, searchSessionId, _yield$Promise$all, _yield$Promise$all2, activityProvider, searchProvider;
        return _regenerator.default.wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              _this$props5 = this.props, timesViewed = _this$props5.timesViewed, inputMethod = _this$props5.inputMethod, searchSessionId = _this$props5.searchSessionId; // Ignored via go/ees005
              // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners, @atlaskit/platform/no-direct-document-usage
              document.addEventListener('mousedown', this.handleClickOutside);
              this.fireAnalytics({
                action: _analytics.ACTION.VIEWED,
                actionSubject: _analytics.ACTION_SUBJECT.CREATE_LINK_INLINE_DIALOG,
                attributes: {
                  timesViewed: timesViewed !== null && timesViewed !== void 0 ? timesViewed : 0,
                  searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
                  trigger: inputMethod !== null && inputMethod !== void 0 ? inputMethod : ''
                },
                eventType: _analytics.EVENT_TYPE.SCREEN
              });
              _context4.next = 5;
              return Promise.all([this.props.activityProvider, this.props.searchProvider]);
            case 5:
              _yield$Promise$all = _context4.sent;
              _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 2);
              activityProvider = _yield$Promise$all2[0];
              searchProvider = _yield$Promise$all2[1];
              // loadInitialLinkSearchResult and updateInput rely on activityProvider being set in state
              (0, _reactDom.flushSync)(function () {
                return _this2.setState({
                  activityProvider: activityProvider,
                  searchProvider: searchProvider
                });
              });
              _context4.next = 12;
              return this.loadInitialLinkSearchResult();
            case 12:
            case "end":
              return _context4.stop();
          }
        }, _callee4, this);
      }));
      function componentDidMount() {
        return _componentDidMount.apply(this, arguments);
      }
      return componentDidMount;
    }()
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var searchSessionId = this.props.searchSessionId;

      // Ignored via go/ees005
      // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners, @atlaskit/platform/no-direct-document-usage
      document.removeEventListener('mousedown', this.handleClickOutside);
      if (!this.submitted) {
        this.fireAnalytics({
          action: _analytics.ACTION.DISMISSED,
          actionSubject: _analytics.ACTION_SUBJECT.CREATE_LINK_INLINE_DIALOG,
          attributes: {
            source: this.analyticSource,
            searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
            trigger: 'blur'
          },
          eventType: _analytics.EVENT_TYPE.UI
        });
      }
    }
  }, {
    key: "getRecentItems",
    value: function () {
      var _getRecentItems = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(activityProvider, query) {
        var searchSessionId, perfStart, activityRecentItems, items, perfStop, duration, _perfStop2, _duration2;
        return _regenerator.default.wrap(function _callee5$(_context5) {
          while (1) switch (_context5.prev = _context5.next) {
            case 0:
              searchSessionId = this.props.searchSessionId;
              perfStart = performance.now();
              _context5.prev = 2;
              _context5.t0 = limit;
              if (!query) {
                _context5.next = 10;
                break;
              }
              _context5.next = 7;
              return activityProvider.searchRecent(query);
            case 7:
              _context5.t1 = _context5.sent;
              _context5.next = 13;
              break;
            case 10:
              _context5.next = 12;
              return activityProvider.getRecentItems();
            case 12:
              _context5.t1 = _context5.sent;
            case 13:
              _context5.t2 = _context5.t1;
              activityRecentItems = (0, _context5.t0)(_context5.t2);
              items = activityRecentItems.map(mapActivityProviderResultToLinkSearchItemData);
              perfStop = performance.now();
              duration = perfStop - perfStart;
              this.fireAnalytics({
                action: _analytics.ACTION.INVOKED,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.RECENT_ACTIVITIES,
                attributes: {
                  duration: duration,
                  count: items.length
                },
                eventType: _analytics.EVENT_TYPE.OPERATIONAL
              });
              this.fireAnalytics({
                action: _analytics.ACTION.SHOWN,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.PRE_QUERY_SEARCH_RESULTS,
                attributes: {
                  source: this.analyticSource,
                  preQueryRequestDurationMs: duration,
                  searchSessionId: searchSessionId !== null && searchSessionId !== void 0 ? searchSessionId : '',
                  resultCount: items.length,
                  results: activityRecentItems.map(function (item) {
                    var _item$type;
                    return {
                      resultContentId: item.objectId,
                      resultType: (_item$type = item.type) !== null && _item$type !== void 0 ? _item$type : ''
                    };
                  })
                },
                eventType: _analytics.EVENT_TYPE.UI
              });
              return _context5.abrupt("return", items);
            case 23:
              _context5.prev = 23;
              _context5.t3 = _context5["catch"](2);
              _perfStop2 = performance.now();
              _duration2 = _perfStop2 - perfStart;
              this.fireAnalytics({
                action: _analytics.ACTION.INVOKED,
                actionSubject: _analytics.ACTION_SUBJECT.SEARCH_RESULT,
                actionSubjectId: _analytics.ACTION_SUBJECT_ID.RECENT_ACTIVITIES,
                attributes: {
                  duration: _duration2,
                  count: -1,
                  errorCode: _context5.t3.status
                },
                eventType: _analytics.EVENT_TYPE.OPERATIONAL
              });
              return _context5.abrupt("return", []);
            case 29:
            case "end":
              return _context5.stop();
          }
        }, _callee5, this, [[2, 23]]);
      }));
      function getRecentItems(_x5, _x6) {
        return _getRecentItems.apply(this, arguments);
      }
      return getRecentItems;
    }()
  }, {
    key: "fireAnalytics",
    value: function fireAnalytics(payload) {
      if (this.props.createAnalyticsEvent && this.fireCustomAnalytics) {
        this.fireCustomAnalytics({
          payload: payload
        });
      }
    }
  }, {
    key: "loadInitialLinkSearchResult",
    value: function () {
      var _loadInitialLinkSearchResult = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6() {
        var _this$state0, displayUrl, activityProvider, items;
        return _regenerator.default.wrap(function _callee6$(_context6) {
          while (1) switch (_context6.prev = _context6.next) {
            case 0:
              _this$state0 = this.state, displayUrl = _this$state0.displayUrl, activityProvider = _this$state0.activityProvider;
              _context6.prev = 1;
              if (!(!displayUrl && activityProvider)) {
                _context6.next = 8;
                break;
              }
              this.setState({
                isLoading: true
              });
              _context6.next = 6;
              return this.getRecentItems(activityProvider);
            case 6:
              items = _context6.sent;
              this.setState({
                items: items
              });
            case 8:
              _context6.prev = 8;
              this.setState({
                isLoading: false
              });
              return _context6.finish(8);
            case 11:
            case "end":
              return _context6.stop();
          }
        }, _callee6, this, [[1,, 8, 11]]);
      }));
      function loadInitialLinkSearchResult() {
        return _loadInitialLinkSearchResult.apply(this, arguments);
      }
      return loadInitialLinkSearchResult;
    }()
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;
      var _this$state1 = this.state,
        items = _this$state1.items,
        isLoading = _this$state1.isLoading,
        selectedIndex = _this$state1.selectedIndex,
        displayUrl = _this$state1.displayUrl,
        displayText = _this$state1.displayText;
      var formatMessage = this.props.intl.formatMessage;
      var formatClearLinkText = formatMessage(messages.clearLink);
      var screenReaderDescriptionId = 'search-recent-links-field-description';
      var linkSearchListId = 'hyperlink-search-list';
      var ariaActiveDescendant = selectedIndex > -1 ? "link-search-list-item-".concat(selectedIndex) : '';
      var linkSearchInputId = 'search-recent-links-field-id';
      var displayTextInputId = 'display-text-filed-id';
      var browser = (0, _expValEquals.expValEquals)('platform_editor_hydratable_ui', 'isEnabled', true) ? (0, _browser.getBrowserInfo)() : _browser.browser;
      // Added workaround with a screen reader Announcer specifically for VoiceOver + Safari
      // as the Aria design pattern for combobox does not work in this case
      // for details: https://a11y-internal.atlassian.net/browse/AK-740
      var screenReaderText = browser.safari && this.getScreenReaderText();
      var hyperlinkElement = (0, _react2.jsx)("div", {
        "aria-label": formatMessage(messages.hyperlinkAriaLabel)
        // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
        ,
        className: "recent-list",
        "data-testid": "hyperlink-add-toolbar"
      }, (0, _react2.jsx)("div", {
        // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766
        css: [_ToolbarComponents.container, _ToolbarComponents.narrowContainerWidth, containerPadding],
        ref: this.wrapperRef
      }, (0, _react2.jsx)("label", {
        htmlFor: linkSearchInputId,
        css: inputLabel
      }, formatMessage(messages.linkVisibleLabel)), (0, _react2.jsx)("div", {
        css: [_ToolbarComponents.inputWrapper, inputWrapperPosition]
      }, screenReaderText && (0, _react2.jsx)(_ui.Announcer, {
        ariaLive: "assertive",
        text: screenReaderText,
        ariaRelevant: "additions",
        delay: 250
      }), (0, _react2.jsx)("div", {
        css: visuallyHiddenStyles,
        "aria-hidden": "true",
        id: screenReaderDescriptionId
      }, formatMessage(messages.searchLinkAriaDescription)), (0, _react2.jsx)(_ui.PanelTextInput, {
        role: "combobox",
        ariaExpanded: (0, _expValEquals.expValEquals)('editor_enghealth_hyperlink_toolbar_aria_values', 'isEnabled', true, false) ? items && items.length > 0 && !isLoading : true,
        ariaActiveDescendant: ariaActiveDescendant,
        ariaControls: linkSearchListId,
        ariaAutoComplete: true,
        describedById: screenReaderDescriptionId
        // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
        ,
        ref: function ref(ele) {
          return _this3.urlInputContainer = ele;
        },
        testId: 'link-url',
        onSubmit: this.handleSubmit,
        onChange: this.updateInput
        // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
        ,
        autoFocus: {
          preventScroll: true
        },
        onCancel: this.handleCancel,
        defaultValue: displayUrl,
        onKeyDown: this.handleKeyDown,
        inputId: linkSearchInputId
      }), displayUrl && (0, _react2.jsx)("div", {
        css: clearTextWrapper
      }, (0, _react2.jsx)(_tooltip.default, {
        content: formatClearLinkText
      }, (0, _react2.jsx)(_primitives.Pressable, {
        xcss: clearTextButtonStyles,
        onClick: this.handleClearText
      }, (0, _react2.jsx)(_crossCircle.default, {
        label: formatClearLinkText,
        color: "var(--ds-icon-subtle, #505258)"
      }))))), (0, _react2.jsx)("label", {
        htmlFor: displayTextInputId,
        css: [inputLabel, textLabelMargin]
      }, formatMessage(messages.textVisibleLabel)), (0, _react2.jsx)("div", {
        css: [_ToolbarComponents.inputWrapper, inputWrapperPosition]
      }, (0, _react2.jsx)(_ui.PanelTextInput
      // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
      , {
        ref: function ref(ele) {
          return _this3.displayTextInputContainer = ele;
        },
        testId: 'link-text',
        onChange: this.updateTextInput,
        onCancel: this.handleCancel,
        defaultValue: displayText,
        onSubmit: this.handleSubmit,
        onKeyDown: this.handleKeyDown,
        inputId: displayTextInputId
      }), displayText && (0, _react2.jsx)("div", {
        css: clearTextWrapper
      }, (0, _react2.jsx)(_tooltip.default, {
        content: formatMessage(messages.clearText)
      }, (0, _react2.jsx)(_primitives.Pressable, {
        xcss: clearTextButtonStyles,
        onClick: this.handleClearDisplayText,
        onKeyDown: this.handleClearTextKeyDown
      }, (0, _react2.jsx)(_crossCircle.default, {
        label: formatMessage(messages.clearText),
        color: "var(--ds-icon-subtle, #505258)"
      }))))), (0, _react2.jsx)("div", {
        css: visuallyHiddenStyles,
        "aria-live": "polite",
        "aria-atomic": "true",
        id: "fabric.editor.hyperlink.suggested.results"
      }, displayUrl && formatMessage(messages.searchLinkResults, {
        count: items.length
      })), (0, _react2.jsx)(_LinkSearchList.default, {
        ariaControls: "fabric.editor.hyperlink.suggested.results",
        id: linkSearchListId,
        role: "listbox",
        items: items,
        isLoading: isLoading,
        selectedIndex: selectedIndex,
        listItemRefCallback: this.listItemRefCallback,
        onFocus: this.handleListItemFocus,
        onBlur: this.handleListItemBlur,
        onKeyDown: this.handleKeyDown,
        onSelect: this.handleSelected,
        onMouseEnter: this.handleMouseEnterResultItem,
        onMouseLeave: this.handleMouseLeaveResultItem
      })));
      if ((0, _expValEquals.expValEquals)('platform_editor_a11y_escape_link_dialog', 'isEnabled', true)) {
        return (
          // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed)
          (0, _react2.jsx)(_reactFocusLock.default, {
            returnFocus: {
              preventScroll: true
            },
            focusOptions: {
              preventScroll: true
            }
          }, hyperlinkElement)
        );
      }
      return hyperlinkElement;
    }
  }]);
}(_react.PureComponent);
function findIndex(array, predicate) {
  var index = -1;
  array.some(function (item, i) {
    if (predicate(item)) {
      index = i;
      return true;
    }
    return false;
  });
  return index;
}
function limit(items) {
  return items.slice(0, RECENT_SEARCH_LIST_SIZE);
}

// eslint-disable-next-line @typescript-eslint/ban-types
var HyperlinkLinkAddToolbarWithIntl = exports.HyperlinkLinkAddToolbarWithIntl = (0, _reactIntlNext.injectIntl)(HyperlinkLinkAddToolbar);
var _default_1 = (0, _withAnalyticsEvents.default)()(HyperlinkLinkAddToolbarWithIntl);
var _default = exports.default = _default_1;