import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import { ResourcedEmoji } from '@atlaskit/emoji/element';
import { ProviderFactory, WithProviders } from '../../provider-factory';
export default class EmojiNode extends PureComponent {
  constructor(props) {
    super(props);
    _defineProperty(this, "renderWithProvider", providers => {
      var _resourceConfig$optim;
      const {
        allowTextFallback,
        shortName,
        id,
        fallback,
        fitToHeight,
        showTooltip,
        resourceConfig
      } = this.props;
      if (allowTextFallback && !providers.emojiProvider) {
        return /*#__PURE__*/React.createElement("span", {
          "data-emoji-id": id,
          "data-emoji-short-name": shortName,
          "data-emoji-text": fallback || shortName
        }, fallback || shortName);
      }
      if (!providers.emojiProvider) {
        return null;
      }
      return /*#__PURE__*/React.createElement(ResourcedEmoji, {
        emojiId: {
          id,
          fallback,
          shortName
        },
        emojiProvider: providers.emojiProvider,
        showTooltip: showTooltip,
        fitToHeight: fitToHeight,
        optimistic: true,
        optimisticImageURL: resourceConfig === null || resourceConfig === void 0 ? void 0 : (_resourceConfig$optim = resourceConfig.optimisticImageApi) === null || _resourceConfig$optim === void 0 ? void 0 : _resourceConfig$optim.getUrl({
          id,
          fallback,
          shortName
        }),
        editorEmoji: true
      });
    });
    this.providerFactory = props.providers || new ProviderFactory();
  }
  componentWillUnmount() {
    if (!this.props.providers) {
      // new ProviderFactory is created if no `providers` has been set
      // in this case when component is unmounted it's safe to destroy this providerFactory
      this.providerFactory.destroy();
    }
  }
  render() {
    return /*#__PURE__*/React.createElement(WithProviders, {
      providers: ['emojiProvider'],
      providerFactory: this.providerFactory,
      renderNode: this.renderWithProvider
    });
  }
}
_defineProperty(EmojiNode, "displayName", 'EmojiNode');
_defineProperty(EmojiNode, "defaultProps", {
  showTooltip: true
});