"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _element = require("@atlaskit/mention/element");
var _mentionWithProfilecard = _interopRequireDefault(require("./mention-with-profilecard"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
var noop = function noop() {};
var MentionWithProviders = exports.default = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2.default)(MentionWithProviders, _PureComponent);
  var _super = _createSuper(MentionWithProviders);
  function MentionWithProviders() {
    var _this;
    (0, _classCallCheck2.default)(this, MentionWithProviders);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _super.call.apply(_super, [this].concat(args));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", {
      profilecardProvider: null
    });
    return _this;
  }
  (0, _createClass2.default)(MentionWithProviders, [{
    key: "UNSAFE_componentWillMount",
    value: function UNSAFE_componentWillMount() {
      this.updateProfilecardProvider(this.props);
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (nextProps.profilecardProvider !== this.props.profilecardProvider) {
        this.updateProfilecardProvider(nextProps);
      }
    }
  }, {
    key: "updateProfilecardProvider",
    value: function updateProfilecardProvider(props) {
      var _this2 = this;
      // 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
      if (props.profilecardProvider) {
        props.profilecardProvider.then(function (profilecardProvider) {
          _this2.setState({
            profilecardProvider: profilecardProvider
          });
        }).catch(function () {
          _this2.setState({
            profilecardProvider: null
          });
        });
      } else {
        this.setState({
          profilecardProvider: null
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        accessLevel = _this$props.accessLevel,
        eventHandlers = _this$props.eventHandlers,
        id = _this$props.id,
        mentionProvider = _this$props.mentionProvider,
        text = _this$props.text;
      var profilecardProvider = this.state.profilecardProvider;
      var actionHandlers = {};
      ['onClick', 'onMouseEnter', 'onMouseLeave'].forEach(function (handler) {
        actionHandlers[handler] = eventHandlers && eventHandlers[handler] || noop;
      });
      var MentionComponent = profilecardProvider && GENERIC_USER_IDS.indexOf(id) === -1 ? _mentionWithProfilecard.default : _element.ResourcedMention;
      return /*#__PURE__*/_react.default.createElement(MentionComponent, (0, _extends2.default)({
        id: id,
        text: text,
        accessLevel: accessLevel,
        mentionProvider: mentionProvider,
        profilecardProvider: profilecardProvider
      }, actionHandlers));
    }
  }]);
  return MentionWithProviders;
}(_react.PureComponent);