import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import { ResourcedMention } from '@atlaskit/mention/element';
import ResourcedMentionWithProfilecard from './mention-with-profilecard';
const GENERIC_USER_IDS = ['HipChat', 'all', 'here'];
const noop = () => {};
export default class MentionWithProviders extends PureComponent {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "state", {
      profilecardProvider: null
    });
  }
  UNSAFE_componentWillMount() {
    this.updateProfilecardProvider(this.props);
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (nextProps.profilecardProvider !== this.props.profilecardProvider) {
      this.updateProfilecardProvider(nextProps);
    }
  }
  updateProfilecardProvider(props) {
    // 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(profilecardProvider => {
        this.setState({
          profilecardProvider
        });
      }).catch(() => {
        this.setState({
          profilecardProvider: null
        });
      });
    } else {
      this.setState({
        profilecardProvider: null
      });
    }
  }
  render() {
    const {
      accessLevel,
      eventHandlers,
      id,
      mentionProvider,
      text
    } = this.props;
    const {
      profilecardProvider
    } = this.state;
    const actionHandlers = {};
    ['onClick', 'onMouseEnter', 'onMouseLeave'].forEach(handler => {
      actionHandlers[handler] = eventHandlers && eventHandlers[handler] || noop;
    });
    const 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));
  }
}