'use strict';

var _rollupPluginBabelHelpers = require('../_rollupPluginBabelHelpers-ea66c137.js');
var React = require('react');
var index$1 = require('../index-358554d2.js');
var ui_Avatar = require('./Avatar.js');
var ui_ContextMenu = require('./ContextMenu.js');
var ui_Icon = require('./Icon.js');
var ui_IconButton = require('./IconButton.js');
var ui_ImageRenderer = require('./ImageRenderer.js');
var ui_LinkLabel = require('./LinkLabel.js');
var ui_Label = require('../index-bff918b2.js');
var ui_Loader = require('./Loader.js');
var ui_UserProfile = require('./UserProfile.js');
var UserProfileContext = require('../UserProfileContext-2ff8462c.js');
var uuid = require('../uuid-f1d9be11.js');
var utils = require('../utils-4a8cff57.js');
var LocalizationContext = require('../LocalizationContext-6adf6aa8.js');
var index = require('../index-fb2bc562.js');
var MediaQueryContext = require('../MediaQueryContext-1dd78ce3.js');
var useLongPress = require('../useLongPress-928530d4.js');
var index$2 = require('../index-c5341a29.js');
var tokenize = require('../tokenize-bc5f4e11.js');
require('../index-6a7f94de.js');
require('prop-types');
require('react-dom');
require('./SortByRow.js');
require('../index-86e680cb.js');
require('../utils/message/getOutgoingMessageState.js');
require('../stringSet-5157206d.js');
require('../sendbirdSelectors.js');
require('../topics-2fe6fef6.js');
require('../utils-58f8eea0.js');
require('./Button.js');
require('../useSendbirdStateContext.js');
require('../withSendbird.js');
require('../index.module-15d2b97f.js');
require('../Message/context.js');
require('./MentionLabel.js');

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

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

const checkOGIsEnalbed = message => {
  const {
    ogMetaData
  } = message;
  if (!ogMetaData) {
    return false;
  }
  const {
    url
  } = ogMetaData;
  if (!url) {
    return false;
  }
  return true;
};

function OpenchannelOGMessage(_ref) {
  var _message$ogMetaData, _message$updatedAt;
  let {
    message,
    isOperator,
    isEphemeral = false,
    className,
    disabled,
    showEdit,
    showRemove,
    resendMessage,
    chainTop,
    userId
  } = _ref;
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const ogMetaData = (_message$ogMetaData = message.ogMetaData) !== null && _message$ogMetaData !== void 0 ? _message$ogMetaData : null;
  const defaultImage = ogMetaData === null || ogMetaData === void 0 ? void 0 : ogMetaData.defaultImage;
  const {
    stringSet,
    dateLocale
  } = LocalizationContext.useLocalization();
  const {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const {
    disableUserProfile,
    renderUserProfile
  } = React.useContext(UserProfileContext.UserProfileContext);
  const [contextStyle, setContextStyle] = React.useState({});
  const [showContextMenu, setShowContextMenu] = React.useState(false);
  const openLink = () => {
    if (checkOGIsEnalbed(message) && ogMetaData !== null && ogMetaData !== void 0 && ogMetaData.url) {
      window.open(ogMetaData.url, '_blank', 'noopener,noreferrer');
    }
  };
  const onLongPress = useLongPress.useLongPress({
    onLongPress: () => setShowContextMenu(true),
    onClick: openLink
  }, {
    delay: 300
  });
  const messageComponentRef = React.useRef(null);
  const contextMenuRef = React.useRef(null);
  const mobileMenuRef = React.useRef(null);
  const avatarRef = React.useRef(null);
  const isPending = index.checkIsPending(status);
  const isFailed = index.checkIsFailed(status);
  const sender = index.getSenderFromMessage(message);
  const tokens = React.useMemo(() => {
    return tokenize.tokenizeMessage({
      messageText: message.message
    });
  }, [message === null || message === void 0 ? void 0 : message.updatedAt]);

  // place conxt menu top depending clientHeight of message component
  React.useEffect(() => {
    var _messageComponentRef$;
    if ((messageComponentRef === null || messageComponentRef === void 0 ? void 0 : (_messageComponentRef$ = messageComponentRef.current) === null || _messageComponentRef$ === void 0 ? void 0 : _messageComponentRef$.clientHeight) > 36) {
      setContextStyle({
        top: '8px '
      });
    } else {
      setContextStyle({
        top: '2px'
      });
    }
  }, [window.innerWidth]);
  if (!message || message.messageType !== 'user') {
    return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null);
  }
  return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers._extends({
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-og-message'].join(' '),
    ref: messageComponentRef
  }, isMobile ? _rollupPluginBabelHelpers._objectSpread2({}, onLongPress) : {}), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top"
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__left"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
      className: "sendbird-openchannel-og-message__top__left__avatar",
      src: sender.profileUrl || '',
      ref: avatarRef,
      width: "28px",
      height: "28px",
      onClick: () => {
        if (!disableUserProfile) {
          toggleDropdown();
        }
      }
    }),
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      parentRef: avatarRef,
      parentContainRef: avatarRef,
      closeDropdown: closeDropdown,
      style: {
        paddingTop: '0px',
        paddingBottom: '0px'
      }
    }, renderUserProfile ? renderUserProfile({
      user: sender,
      close: closeDropdown
    }) : /*#__PURE__*/React__default["default"].createElement(ui_UserProfile, {
      user: sender,
      onSuccess: closeDropdown,
      disableMessaging: true
    }))
  })), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__right"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__title"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-og-message__top__right__title__sender-name",
    type: ui_Label.LabelTypography.CAPTION_2,
    color: isOperator ? ui_Label.LabelColors.SECONDARY_3 : ui_Label.LabelColors.ONBACKGROUND_2
  }, sender && (sender.friendName || sender.nickname || sender.userId)), /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-og-message__top__right__title__sent-at",
    type: ui_Label.LabelTypography.CAPTION_3,
    color: ui_Label.LabelColors.ONBACKGROUND_3
  }, (message === null || message === void 0 ? void 0 : message.createdAt) && index$1.format(message === null || message === void 0 ? void 0 : message.createdAt, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__description"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-og-message__top__right__description__message",
    type: ui_Label.LabelTypography.BODY_1,
    color: ui_Label.LabelColors.ONBACKGROUND_1
  }, /*#__PURE__*/React__default["default"].createElement(index$2.TextFragment, {
    tokens: tokens
  }), ((_message$updatedAt = message === null || message === void 0 ? void 0 : message.updatedAt) !== null && _message$updatedAt !== void 0 ? _message$updatedAt : 0) > 0 && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    key: uuid.uuidv4(),
    className: "sendbird-openchannel-og-message--word",
    type: ui_Label.LabelTypography.BODY_1,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, stringSet.MESSAGE_EDITED)))), !isMobile && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__context-menu",
    ref: contextMenuRef,
    style: contextStyle
  }, /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => index.showMenuTrigger({
      message: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_IconButton, {
      className: "sendbird-openchannel-og-message__top__context-menu--icon",
      width: "32px",
      height: "32px",
      onClick: () => {
        toggleDropdown();
      }
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      type: ui_Icon.IconTypes.MORE,
      fillColor: ui_Icon.IconColors.CONTENT_INVERSE,
      width: "24px",
      height: "24px"
    })),
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      parentRef: contextMenuRef,
      parentContainRef: contextMenuRef,
      closeDropdown: closeDropdown,
      openLeft: true
    }, index.isFineCopy({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__copy",
      onClick: () => {
        utils.copyToClipboard(message.message);
        closeDropdown();
      },
      dataSbId: "open_channel_og_message_menu_copy"
    }, stringSet.CONTEXT_MENU_DROPDOWN__COPY), !isEphemeral && index.isFineEdit({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__edit",
      onClick: () => {
        if (disabled) {
          return;
        }
        showEdit(true);
        closeDropdown();
      },
      dataSbId: "open_channel_og_message_menu_edit"
    }, stringSet.CONTEXT_MENU_DROPDOWN__EDIT), index.isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__resend",
      onClick: () => {
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_og_message_menu_resend"
    }, stringSet.CONTEXT_MENU_DROPDOWN__RESEND), !isEphemeral && index.isFineDelete({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__delete",
      onClick: () => {
        if (disabled) {
          return;
        }
        showRemove(true);
        closeDropdown();
      },
      dataSbId: "open_channel_og_message_menu_delete"
    }, stringSet.CONTEXT_MENU_DROPDOWN__DELETE))
  }))), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__bottom"
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__bottom__og-tag",
    ref: mobileMenuRef
  }, ogMetaData.url && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__url",
    type: ui_Label.LabelTypography.CAPTION_3,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, ogMetaData.url), ogMetaData.title && /*#__PURE__*/React__default["default"].createElement(ui_LinkLabel["default"], {
    className: "sendbird-openchannel-og-message__bottom__og-tag__title",
    src: ogMetaData.url,
    type: ui_Label.LabelTypography.SUBTITLE_2,
    color: ui_Label.LabelColors.PRIMARY
  }, ogMetaData.title), ogMetaData.description && /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__description",
    type: ui_Label.LabelTypography.BODY_2,
    color: ui_Label.LabelColors.ONBACKGROUND_1
  }, ogMetaData.description), ogMetaData.url && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail",
    role: "button",
    onClick: openLink,
    onKeyDown: openLink,
    tabIndex: 0
  }, defaultImage && /*#__PURE__*/React__default["default"].createElement(ui_ImageRenderer, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail__image",
    url: defaultImage.url || '',
    alt: defaultImage.alt || '',
    height: "189px",
    defaultComponent: /*#__PURE__*/React__default["default"].createElement("div", {
      className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail__image--placeholder"
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      type: ui_Icon.IconTypes.THUMBNAIL_NONE,
      width: "56px",
      height: "56px"
    }))
  }))), (isPending || isFailed) && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__tail"
  }, isPending && /*#__PURE__*/React__default["default"].createElement(ui_Loader, {
    width: "16px",
    height: "16px"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
    className: "sendbird-openchannel-og-message__top__right__tail__pending",
    type: ui_Icon.IconTypes.SPINNER,
    fillColor: ui_Icon.IconColors.PRIMARY,
    width: "16px",
    height: "16px"
  })), isFailed && /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
    className: "sendbird-openchannel-og-message__top__right__tail__failed",
    type: ui_Icon.IconTypes.ERROR,
    fillColor: ui_Icon.IconColors.ERROR,
    width: "16px",
    height: "16px"
  })))), showContextMenu && /*#__PURE__*/React__default["default"].createElement(index.OpenChannelMobileMenu, {
    message: message,
    parentRef: mobileMenuRef,
    hideMenu: () => {
      setShowContextMenu(false);
    },
    showRemove: () => {
      setShowContextMenu(false);
      showRemove(true);
    },
    showEdit: () => {
      setShowContextMenu(false);
      showEdit(true);
    },
    copyToClipboard: () => {
      setShowContextMenu(false);
      utils.copyToClipboard(message === null || message === void 0 ? void 0 : message.message);
    },
    resendMessage: () => {
      setShowContextMenu(false);
      resendMessage(message);
    }
  }));
}

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