"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 _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/emoji/element");
var _providerFactory = require("../../provider-factory");
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 EmojiNode = exports.default = /*#__PURE__*/function (_PureComponent) {
  (0, _inherits2.default)(EmojiNode, _PureComponent);
  var _super = _createSuper(EmojiNode);
  function EmojiNode(props) {
    var _this;
    (0, _classCallCheck2.default)(this, EmojiNode);
    _this = _super.call(this, props);
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_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.default.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.default.createElement(_element.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.ProviderFactory();
    return _this;
  }
  (0, _createClass2.default)(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.default.createElement(_providerFactory.WithProviders, {
        providers: ['emojiProvider'],
        providerFactory: this.providerFactory,
        renderNode: this.renderWithProvider
      });
    }
  }]);
  return EmojiNode;
}(_react.PureComponent);
(0, _defineProperty2.default)(EmojiNode, "displayName", 'EmojiNode');
(0, _defineProperty2.default)(EmojiNode, "defaultProps", {
  showTooltip: true
});