import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { PureComponent } from 'react';
import { ResourcedMention } from '@atlaskit/mention/element';
import ResourcedMentionWithProfilecard from './mention-with-profilecard';
var GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
var noop = function noop() {};
var MentionWithProviders = /*#__PURE__*/function (_PureComponent) {
  _inherits(MentionWithProviders, _PureComponent);
  var _super = _createSuper(MentionWithProviders);
  function MentionWithProviders() {
    var _this;
    _classCallCheck(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));
    _defineProperty(_assertThisInitialized(_this), "state", {
      profilecardProvider: null
    });
    return _this;
  }
  _createClass(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 ? ResourcedMentionWithProfilecard : ResourcedMention;
      return /*#__PURE__*/React.createElement(MentionComponent, _extends({
        id: id,
        text: text,
        accessLevel: accessLevel,
        mentionProvider: mentionProvider,
        profilecardProvider: profilecardProvider
      }, actionHandlers));
    }
  }]);
  return MentionWithProviders;
}(PureComponent);
export { MentionWithProviders as default };