import React__default, { useRef, useContext, useMemo } from 'react';
import ImageRenderer from './ImageRenderer.js';
import Icon, { IconTypes } from './Icon.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../index-4105bc4d.js';
import { h as getClassName, B as isEditedMessage } from '../index-f83f0aac.js';
import { a as LocalizationContext } from '../LocalizationContext-426f3d58.js';
import { T as TextFragment } from '../index-89c84c27.js';
import { t as tokenizeMessage } from '../tokenize-eec42500.js';
import { O as OG_MESSAGE_BODY_CLASSNAME } from '../consts-2eeaa3de.js';
import 'prop-types';
import '../stringSet-a62108b2.js';
import '../_rollupPluginBabelHelpers-a69fd1ce.js';
import '../utils/message/getOutgoingMessageState.js';
import '../index-90420b99.js';
import '../index.module-8bb7760e.js';
import '../Message/context.js';
import './MentionLabel.js';
import './ContextMenu.js';
import 'react-dom';
import './SortByRow.js';
import '../uuid-2e8efc75.js';
import './UserProfile.js';
import '../UserProfileContext-a59f0533.js';
import '../sendbirdSelectors.js';
import '../topics-247fcf3a.js';
import '../utils-4bf6e4e6.js';
import './Avatar.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';
import './LinkLabel.js';

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 = useRef(null);
  const {
    stringSet
  } = useContext(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 = useMemo(() => {
    if (isMessageMentioned) {
      return tokenizeMessage({
        mentionedUsers: message === null || message === void 0 ? void 0 : message.mentionedUsers,
        messageText: message === null || message === void 0 ? void 0 : message.mentionedMessageTemplate
      });
    }
    return tokenizeMessage({
      messageText: message === null || message === void 0 ? void 0 : message.message
    });
  }, [message === null || message === void 0 ? void 0 : message.updatedAt]);
  return /*#__PURE__*/React__default.createElement("div", {
    className: 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.createElement(Label, {
    type: LabelTypography.BODY_1,
    color: isByMe ? LabelColors.ONCONTENT_1 : LabelColors.ONBACKGROUND_1
  }, /*#__PURE__*/React__default.createElement("div", {
    className: OG_MESSAGE_BODY_CLASSNAME
  }, /*#__PURE__*/React__default.createElement(TextFragment, {
    tokens: tokens
  }), isEditedMessage(message) && /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-og-message-item-body__text-bubble__message",
    type: LabelTypography.BODY_1,
    color: isByMe ? LabelColors.ONCONTENT_2 : LabelColors.ONBACKGROUND_2
  }, ` ${stringSet.MESSAGE_EDITED} `))), /*#__PURE__*/React__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.createElement(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.createElement("div", {
      className: "sendbird-og-message-item-body__og-thumbnail__place-holder"
    }, /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-og-message-item-body__og-thumbnail__place-holder__icon",
      type: IconTypes.THUMBNAIL_NONE,
      width: "56px",
      height: "56px"
    }))
  })), /*#__PURE__*/React__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.createElement(Label, {
    className: "sendbird-og-message-item-body__description__title",
    type: LabelTypography.SUBTITLE_2,
    color: 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.createElement(Label, {
    className: "sendbird-og-message-item-body__description__description",
    type: LabelTypography.BODY_2,
    color: 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.createElement(Label, {
    className: "sendbird-og-message-item-body__description__url",
    type: LabelTypography.CAPTION_3,
    color: LabelColors.ONBACKGROUND_2
  }, message.ogMetaData.url)), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-og-message-item-body__cover"
  }));
}

export { OGMessageItemBody as default };
//# sourceMappingURL=OGMessageItemBody.js.map
