import { b as _extends, _ as _objectSpread2 } from '../_rollupPluginBabelHelpers-4f8ebde4.js';
import React__default, { useContext, useState, useRef, useMemo, useEffect } from 'react';
import { f as format } from '../index-2dc53ac2.js';
import Avatar from './Avatar.js';
import ContextMenu, { MenuItems, MenuItem } from './ContextMenu.js';
import Icon, { IconTypes, IconColors } from './Icon.js';
import IconButton from './IconButton.js';
import ImageRenderer from './ImageRenderer.js';
import LinkLabel from './LinkLabel.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../index-4372fcec.js';
import Loader from './Loader.js';
import UserProfile from './UserProfile.js';
import { a as UserProfileContext } from '../UserProfileContext-d5787c95.js';
import { u as uuidv4 } from '../uuid-a79b5ba8.js';
import { c as copyToClipboard } from '../utils-3b418b35.js';
import { u as useLocalization } from '../LocalizationContext-b2390bf8.js';
import { g as getSenderFromMessage, s as showMenuTrigger, i as isFineCopy, a as isFineEdit, b as isFineResend, c as isFineDelete, O as OpenChannelMobileMenu, d as checkIsPending, e as checkIsFailed } from '../index-1fefb8e6.js';
import { u as useMediaQueryContext } from '../MediaQueryContext-b4904a2e.js';
import { u as useLongPress } from '../useLongPress-cb2d23be.js';
import { T as TextFragment } from '../index-8aa7880e.js';
import { t as tokenizeMessage } from '../tokenize-b97bc343.js';
import '../index-67135027.js';
import 'prop-types';
import 'react-dom';
import './SortByRow.js';
import '../index-eefb44ba.js';
import '../utils/message/getOutgoingMessageState.js';
import '../stringSet-eefbc798.js';
import '../sendbirdSelectors.js';
import '../topics-f3629ea0.js';
import '../utils-b53fe532.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';
import '../index.module-2885df8c.js';
import '../Message/context.js';
import './MentionLabel.js';

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
  } = useLocalization();
  const {
    isMobile
  } = useMediaQueryContext();
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(UserProfileContext);
  const [contextStyle, setContextStyle] = useState({});
  const [showContextMenu, setShowContextMenu] = useState(false);
  const openLink = () => {
    if (checkOGIsEnalbed(message) && ogMetaData !== null && ogMetaData !== void 0 && ogMetaData.url) {
      window.open(ogMetaData.url, '_blank', 'noopener,noreferrer');
    }
  };
  const onLongPress = useLongPress({
    onLongPress: () => setShowContextMenu(true),
    onClick: openLink
  }, {
    delay: 300
  });
  const messageComponentRef = useRef(null);
  const contextMenuRef = useRef(null);
  const mobileMenuRef = useRef(null);
  const avatarRef = useRef(null);
  const isPending = checkIsPending(status);
  const isFailed = checkIsFailed(status);
  const sender = getSenderFromMessage(message);
  const tokens = useMemo(() => {
    return tokenizeMessage({
      messageText: message.message
    });
  }, [message === null || message === void 0 ? void 0 : message.updatedAt]);

  // place conxt menu top depending clientHeight of message component
  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.createElement(React__default.Fragment, null);
  }
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", _extends({
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-og-message'].join(' '),
    ref: messageComponentRef
  }, isMobile ? _objectSpread2({}, onLongPress) : {}), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__left"
  }, !chainTop && /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(Avatar, {
      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.createElement(MenuItems, {
      parentRef: avatarRef,
      parentContainRef: avatarRef,
      closeDropdown: closeDropdown,
      style: {
        paddingTop: '0px',
        paddingBottom: '0px'
      }
    }, renderUserProfile ? renderUserProfile({
      user: sender,
      close: closeDropdown
    }) : /*#__PURE__*/React__default.createElement(UserProfile, {
      user: sender,
      onSuccess: closeDropdown,
      disableMessaging: true
    }))
  })), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__right"
  }, !chainTop && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__title"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-og-message__top__right__title__sender-name",
    type: LabelTypography.CAPTION_2,
    color: isOperator ? LabelColors.SECONDARY_3 : LabelColors.ONBACKGROUND_2
  }, sender && (sender.friendName || sender.nickname || sender.userId)), /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-og-message__top__right__title__sent-at",
    type: LabelTypography.CAPTION_3,
    color: LabelColors.ONBACKGROUND_3
  }, (message === null || message === void 0 ? void 0 : message.createdAt) && format(message === null || message === void 0 ? void 0 : message.createdAt, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__description"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-og-message__top__right__description__message",
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_1
  }, /*#__PURE__*/React__default.createElement(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.createElement(Label, {
    key: uuidv4(),
    className: "sendbird-openchannel-og-message--word",
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_2
  }, stringSet.MESSAGE_EDITED)))), !isMobile && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__context-menu",
    ref: contextMenuRef,
    style: contextStyle
  }, /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => showMenuTrigger({
      message: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default.createElement(IconButton, {
      className: "sendbird-openchannel-og-message__top__context-menu--icon",
      width: "32px",
      height: "32px",
      onClick: () => {
        toggleDropdown();
      }
    }, /*#__PURE__*/React__default.createElement(Icon, {
      type: IconTypes.MORE,
      fillColor: IconColors.CONTENT_INVERSE,
      width: "24px",
      height: "24px"
    })),
    menuItems: closeDropdown => /*#__PURE__*/React__default.createElement(MenuItems, {
      parentRef: contextMenuRef,
      parentContainRef: contextMenuRef,
      closeDropdown: closeDropdown,
      openLeft: true
    }, isFineCopy({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__copy",
      onClick: () => {
        copyToClipboard(message.message);
        closeDropdown();
      },
      dataSbId: "open_channel_og_message_menu_copy"
    }, stringSet.CONTEXT_MENU_DROPDOWN__COPY), !isEphemeral && isFineEdit({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(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), isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(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 && isFineDelete({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(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.createElement("div", {
    className: "sendbird-openchannel-og-message__bottom"
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__bottom__og-tag",
    ref: mobileMenuRef
  }, ogMetaData.url && /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__url",
    type: LabelTypography.CAPTION_3,
    color: LabelColors.ONBACKGROUND_2
  }, ogMetaData.url), ogMetaData.title && /*#__PURE__*/React__default.createElement(LinkLabel, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__title",
    src: ogMetaData.url,
    type: LabelTypography.SUBTITLE_2,
    color: LabelColors.PRIMARY
  }, ogMetaData.title), ogMetaData.description && /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__description",
    type: LabelTypography.BODY_2,
    color: LabelColors.ONBACKGROUND_1
  }, ogMetaData.description), ogMetaData.url && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail",
    role: "button",
    onClick: openLink,
    onKeyDown: openLink,
    tabIndex: 0
  }, defaultImage && /*#__PURE__*/React__default.createElement(ImageRenderer, {
    className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail__image",
    url: defaultImage.url || '',
    alt: defaultImage.alt || '',
    height: "189px",
    defaultComponent: /*#__PURE__*/React__default.createElement("div", {
      className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail__image--placeholder"
    }, /*#__PURE__*/React__default.createElement(Icon, {
      type: IconTypes.THUMBNAIL_NONE,
      width: "56px",
      height: "56px"
    }))
  }))), (isPending || isFailed) && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-og-message__top__right__tail"
  }, isPending && /*#__PURE__*/React__default.createElement(Loader, {
    width: "16px",
    height: "16px"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-og-message__top__right__tail__pending",
    type: IconTypes.SPINNER,
    fillColor: IconColors.PRIMARY,
    width: "16px",
    height: "16px"
  })), isFailed && /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-og-message__top__right__tail__failed",
    type: IconTypes.ERROR,
    fillColor: IconColors.ERROR,
    width: "16px",
    height: "16px"
  })))), showContextMenu && /*#__PURE__*/React__default.createElement(OpenChannelMobileMenu, {
    message: message,
    parentRef: mobileMenuRef,
    hideMenu: () => {
      setShowContextMenu(false);
    },
    showRemove: () => {
      setShowContextMenu(false);
      showRemove(true);
    },
    showEdit: () => {
      setShowContextMenu(false);
      showEdit(true);
    },
    copyToClipboard: () => {
      setShowContextMenu(false);
      copyToClipboard(message === null || message === void 0 ? void 0 : message.message);
    },
    resendMessage: () => {
      setShowContextMenu(false);
      resendMessage(message);
    }
  }));
}

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