"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MentionWithProviders = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _element = require("@atlaskit/mention/element");
var _mentionWithProfilecard = _interopRequireDefault(require("./mention-with-profilecard"));
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 GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
var MentionWithProviders = exports.MentionWithProviders = /*#__PURE__*/_react.default.memo(function (_ref) {
  var accessLevel = _ref.accessLevel,
    disabledTooltip = _ref.disabledTooltip,
    eventHandlers = _ref.eventHandlers,
    id = _ref.id,
    isDisabled = _ref.isDisabled,
    mentionProvider = _ref.mentionProvider,
    profilecardProviderResolver = _ref.profilecardProvider,
    text = _ref.text,
    localId = _ref.localId,
    userType = _ref.userType;
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    profilecardProvider = _useState2[0],
    setProfilecardProvider = _useState2[1];
  var mountedRef = (0, _react.useRef)(true);
  (0, _react.useLayoutEffect)(function () {
    mountedRef.current = true;
    return function () {
      mountedRef.current = false;
    };
  }, []);
  (0, _react.useLayoutEffect)(function () {
    // We are not using async/await here to avoid having an intermediate Promise
    // introduced by the transpiler.
    // This will allow consumer to use a SynchronousPromise.resolve and avoid useless
    // rerendering
    profilecardProviderResolver === null || profilecardProviderResolver === void 0 || profilecardProviderResolver.then(function (result) {
      if (mountedRef.current) {
        setProfilecardProvider(result);
      }
    }).catch(function () {
      if (mountedRef.current) {
        setProfilecardProvider(null);
      }
    });
  }, [profilecardProviderResolver]);
  var MentionComponent = profilecardProvider && profilecardProviderResolver && GENERIC_USER_IDS.indexOf(id) === -1 ? _mentionWithProfilecard.default : _element.ResourcedMention;
  var ssrPlaceholderId = "mention-".concat(id);

  // A disabled mention is non-interactive, so it bypasses the profile-card
  // wrapper and renders the disabled resourced mention directly.
  if (isDisabled) {
    return /*#__PURE__*/_react.default.createElement(_element.ResourcedMention, {
      id: id,
      text: text,
      accessLevel: accessLevel,
      localId: localId,
      mentionProvider: mentionProvider,
      isDisabled: true,
      disabledTooltip: disabledTooltip,
      onClick: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onClick,
      onMouseEnter: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseEnter,
      onMouseLeave: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseLeave,
      ssrPlaceholderId: ssrPlaceholderId
    });
  }
  return /*#__PURE__*/_react.default.createElement(MentionComponent, {
    id: id,
    text: text,
    accessLevel: accessLevel,
    localId: localId,
    userType: userType,
    mentionProvider: mentionProvider
    // Ignored via go/ees005
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    ,
    profilecardProvider: profilecardProvider,
    onClick: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onClick,
    onMouseEnter: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseEnter,
    onMouseLeave: eventHandlers === null || eventHandlers === void 0 ? void 0 : eventHandlers.onMouseLeave,
    ssrPlaceholderId: ssrPlaceholderId
  });
});