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 { ResourcedEmoji } from '@atlaskit/emoji/element';
import { ProviderFactory, WithProviders } from '../../provider-factory';
var EmojiNode = /*#__PURE__*/function (_PureComponent) {
  _inherits(EmojiNode, _PureComponent);
  var _super = _createSuper(EmojiNode);
  function EmojiNode(props) {
    var _this;
    _classCallCheck(this, EmojiNode);
    _this = _super.call(this, props);
    _defineProperty(_assertThisInitialized(_this), "renderWithProvider", function (providers) {
      var _resourceConfig$optim;
      var _this$props = _this.props,
        allowTextFallback = _this$props.allowTextFallback,
        shortName = _this$props.shortName,
        id = _this$props.id,
        fallback = _this$props.fallback,
        fitToHeight = _this$props.fitToHeight,
        showTooltip = _this$props.showTooltip,
        resourceConfig = _this$props.resourceConfig;
      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: id,
          fallback: fallback,
          shortName: shortName
        },
        emojiProvider: providers.emojiProvider,
        showTooltip: showTooltip,
        fitToHeight: fitToHeight,
        optimistic: true,
        optimisticImageURL: resourceConfig === null || resourceConfig === void 0 || (_resourceConfig$optim = resourceConfig.optimisticImageApi) === null || _resourceConfig$optim === void 0 ? void 0 : _resourceConfig$optim.getUrl({
          id: id,
          fallback: fallback,
          shortName: shortName
        }),
        editorEmoji: true
      });
    });
    _this.providerFactory = props.providers || new ProviderFactory();
    return _this;
  }
  _createClass(EmojiNode, [{
    key: "componentWillUnmount",
    value: function 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();
      }
    }
  }, {
    key: "render",
    value: function render() {
      return /*#__PURE__*/React.createElement(WithProviders, {
        providers: ['emojiProvider'],
        providerFactory: this.providerFactory,
        renderNode: this.renderWithProvider
      });
    }
  }]);
  return EmojiNode;
}(PureComponent);
_defineProperty(EmojiNode, "displayName", 'EmojiNode');
_defineProperty(EmojiNode, "defaultProps", {
  showTooltip: true
});
export { EmojiNode as default };