'use strict';

var React = require('react');
var ui_ImageRenderer = require('./ImageRenderer.js');
var ui_Icon = require('./Icon.js');
var ui_Label = require('../index-2bfd366f.js');
var index = require('../index-1b3c69da.js');
var LocalizationContext = require('../LocalizationContext-44d2f61b.js');
var index$1 = require('../index-8cd9b25d.js');
var tokenize = require('../tokenize-44139f1e.js');
var consts = require('../consts-bce71b24.js');
require('prop-types');
require('../stringSet-7aebf034.js');
require('../_rollupPluginBabelHelpers-21ae158b.js');
require('../utils/message/getOutgoingMessageState.js');
require('../index-88e8fdfa.js');
require('../index.module-5a6a79b6.js');
require('../Message/context.js');
require('./MentionLabel.js');
require('./ContextMenu.js');
require('react-dom');
require('./SortByRow.js');
require('../uuid-dee74248.js');
require('./UserProfile.js');
require('../UserProfileContext-9d843d68.js');
require('../sendbirdSelectors.js');
require('../topics-373d2156.js');
require('../utils-6a748c02.js');
require('./Avatar.js');
require('./Button.js');
require('../useSendbirdStateContext.js');
require('../withSendbird.js');
require('./LinkLabel.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function OGMessageItemBody(_ref) {
  var _message$mentionedMes, _message$mentionedUse, _message$reactions, _message$ogMetaData3, _message$ogMetaData3$, _message$ogMetaData4, _message$ogMetaData4$, _message$ogMetaData5, _message$ogMetaData5$, _message$ogMetaData6, _message$ogMetaData7, _message$ogMetaData8;
  let {
    className,
    message,
    isByMe = false,
    mouseHover = false,
    isMentionEnabled = false,
    isReactionEnabled = false,
    onMessageHeightChange = () => {/* noop */}
  } = _ref;
  const imageRef = React.useRef(null);
  const {
    stringSet
  } = React.useContext(LocalizationContext.LocalizationContext);
  const openOGUrl = () => {
    var _message$ogMetaData, _message$ogMetaData2;
    if (message !== null && message !== void 0 && (_message$ogMetaData = message.ogMetaData) !== null && _message$ogMetaData !== void 0 && _message$ogMetaData.url) window.open(message === null || message === void 0 ? void 0 : (_message$ogMetaData2 = message.ogMetaData) === null || _message$ogMetaData2 === void 0 ? void 0 : _message$ogMetaData2.url);
  };
  const isMessageMentioned = isMentionEnabled && (message === null || message === void 0 ? void 0 : (_message$mentionedMes = message.mentionedMessageTemplate) === null || _message$mentionedMes === void 0 ? void 0 : _message$mentionedMes.length) > 0 && (message === null || message === void 0 ? void 0 : (_message$mentionedUse = message.mentionedUsers) === null || _message$mentionedUse === void 0 ? void 0 : _message$mentionedUse.length) > 0;
  const tokens = React.useMemo(() => {
    if (isMessageMentioned) {
      return tokenize.tokenizeMessage({
        mentionedUsers: message === null || message === void 0 ? void 0 : message.mentionedUsers,
        messageText: message === null || message === void 0 ? void 0 : message.mentionedMessageTemplate
      });
    }
    return tokenize.tokenizeMessage({
      messageText: message === null || message === void 0 ? void 0 : message.message
    });
  }, [message === null || message === void 0 ? void 0 : message.updatedAt]);
  return /*#__PURE__*/React__default["default"].createElement("div", {
    className: index.getClassName([className, 'sendbird-og-message-item-body', isByMe ? 'outgoing' : 'incoming', mouseHover ? 'mouse-hover' : '', isReactionEnabled && (message === null || message === void 0 ? void 0 : (_message$reactions = message.reactions) === null || _message$reactions === void 0 ? void 0 : _message$reactions.length) > 0 ? 'reactions' : ''])
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    type: ui_Label.LabelTypography.BODY_1,
    color: isByMe ? ui_Label.LabelColors.ONCONTENT_1 : ui_Label.LabelColors.ONBACKGROUND_1
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: consts.OG_MESSAGE_BODY_CLASSNAME
  }, /*#__PURE__*/React__default["default"].createElement(index$1.TextFragment, {
    tokens: tokens
  }), index.isEditedMessage(message) && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-og-message-item-body__text-bubble__message",
    type: ui_Label.LabelTypography.BODY_1,
    color: isByMe ? ui_Label.LabelColors.ONCONTENT_2 : ui_Label.LabelColors.ONBACKGROUND_2
  }, ` ${stringSet.MESSAGE_EDITED} `))), /*#__PURE__*/React__default["default"].createElement("div", {
    ref: imageRef,
    className: `sendbird-og-message-item-body__og-thumbnail
          ${message !== null && message !== void 0 && (_message$ogMetaData3 = message.ogMetaData) !== null && _message$ogMetaData3 !== void 0 && (_message$ogMetaData3$ = _message$ogMetaData3.defaultImage) !== null && _message$ogMetaData3$ !== void 0 && _message$ogMetaData3$.url ? '' : 'sendbird-og-message-item-body__og-thumbnail__empty'}
        `,
    onClick: openOGUrl
  }, /*#__PURE__*/React__default["default"].createElement(ui_ImageRenderer, {
    onLoad: onMessageHeightChange,
    onError: () => {
      try {
        var _imageRef$current, _imageRef$current$cla;
        imageRef === null || imageRef === void 0 ? void 0 : (_imageRef$current = imageRef.current) === null || _imageRef$current === void 0 ? void 0 : (_imageRef$current$cla = _imageRef$current.classList) === null || _imageRef$current$cla === void 0 ? void 0 : _imageRef$current$cla.add('sendbird-og-message-item-body__og-thumbnail__empty');
      } catch (error) {
        // do nothing
      }
    },
    className: "sendbird-og-message-item-body__og-thumbnail__image",
    url: (message === null || message === void 0 ? void 0 : (_message$ogMetaData4 = message.ogMetaData) === null || _message$ogMetaData4 === void 0 ? void 0 : (_message$ogMetaData4$ = _message$ogMetaData4.defaultImage) === null || _message$ogMetaData4$ === void 0 ? void 0 : _message$ogMetaData4$.url) || '',
    alt: message === null || message === void 0 ? void 0 : (_message$ogMetaData5 = message.ogMetaData) === null || _message$ogMetaData5 === void 0 ? void 0 : (_message$ogMetaData5$ = _message$ogMetaData5.defaultImage) === null || _message$ogMetaData5$ === void 0 ? void 0 : _message$ogMetaData5$.alt
    // TODO: Change fixing width and height lengths
    ,
    defaultComponent: /*#__PURE__*/React__default["default"].createElement("div", {
      className: "sendbird-og-message-item-body__og-thumbnail__place-holder"
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-og-message-item-body__og-thumbnail__place-holder__icon",
      type: ui_Icon.IconTypes.THUMBNAIL_NONE,
      width: "56px",
      height: "56px"
    }))
  })), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-og-message-item-body__description",
    onClick: openOGUrl
  }, (message === null || message === void 0 ? void 0 : (_message$ogMetaData6 = message.ogMetaData) === null || _message$ogMetaData6 === void 0 ? void 0 : _message$ogMetaData6.title) && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-og-message-item-body__description__title",
    type: ui_Label.LabelTypography.SUBTITLE_2,
    color: ui_Label.LabelColors.ONBACKGROUND_1
  }, message.ogMetaData.title), (message === null || message === void 0 ? void 0 : (_message$ogMetaData7 = message.ogMetaData) === null || _message$ogMetaData7 === void 0 ? void 0 : _message$ogMetaData7.description) && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-og-message-item-body__description__description",
    type: ui_Label.LabelTypography.BODY_2,
    color: ui_Label.LabelColors.ONBACKGROUND_1
  }, message.ogMetaData.description), (message === null || message === void 0 ? void 0 : (_message$ogMetaData8 = message.ogMetaData) === null || _message$ogMetaData8 === void 0 ? void 0 : _message$ogMetaData8.url) && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-og-message-item-body__description__url",
    type: ui_Label.LabelTypography.CAPTION_3,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, message.ogMetaData.url)), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-og-message-item-body__cover"
  }));
}

module.exports = OGMessageItemBody;
//# sourceMappingURL=OGMessageItemBody.js.map
