import { b as _extends, _ as _objectSpread2 } from '../_rollupPluginBabelHelpers-b2d9bfc5.js';
import React__default, { useContext, useState, useRef, useMemo, useEffect } from 'react';
import { f as format } from '../index-f48cfdf7.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-4111594a.js';
import Loader from './Loader.js';
import UserProfile from './UserProfile.js';
import { a as UserProfileContext } from '../UserProfileContext-a8aa4c0f.js';
import { u as uuidv4 } from '../uuid-48baadaf.js';
import { c as copyToClipboard } from '../utils-83bbfb63.js';
import { u as useLocalization } from '../LocalizationContext-9c54e783.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-c7cc73db.js';
import { u as useMediaQueryContext } from '../MediaQueryContext-1988a4bc.js';
import { u as useLongPress } from '../useLongPress-8acf717c.js';
import { T as TextFragment } from '../index-8df6bff0.js';
import { t as tokenizeMessage } from '../tokenize-63d895d3.js';
import '../index-a9ddd0f5.js';
import 'prop-types';
import 'react-dom';
import './SortByRow.js';
import '../index-dbe0e004.js';
import '../utils/message/getOutgoingMessageState.js';
import '../stringSet-1f18348a.js';
import '../sendbirdSelectors.js';
import '../topics-d8d10f83.js';
import '../utils-0c09801e.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';
import '../index.module-ab8289c1.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) {
  let {
    message,
    isOperator,
    isEphemeral = false,
    className,
    disabled,
    showEdit,
    showRemove,
    resendMessage,
    chainTop,
    userId
  } = _ref;
  if (!message || message.messageType !== 'user') {
    return null;
  }
  const openLink = () => {
    if (checkOGIsEnalbed(message)) {
      const {
        url
      } = ogMetaData;
      window.open(url);
    }
  };
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const {
    ogMetaData
  } = message;
  const {
    defaultImage
  } = ogMetaData;
  const {
    stringSet,
    dateLocale
  } = useLocalization();
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(UserProfileContext);
  const [contextStyle, setContextStyle] = useState({});
  const [contextMenu, setContextMenu] = useState(false);
  const onLongPress = useLongPress({
    onLongPress: () => setContextMenu(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 {
    isMobile
  } = useMediaQueryContext();
  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]);
  return /*#__PURE__*/React__default.createElement("div", {
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-og-message'].join(' '),
    ref: messageComponentRef
  }, /*#__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 === null || message === void 0 ? void 0 : message.updatedAt) > 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();
      }
    }, 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();
      }
    }, 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();
      }
    }, 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();
      }
    }, 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", _extends({
    className: "sendbird-openchannel-og-message__bottom__og-tag__thumbnail",
    role: "button",
    onClick: openLink,
    onKeyDown: openLink,
    tabIndex: 0
  }, isMobile ? _objectSpread2({}, onLongPress) : {}), 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"
  }))), contextMenu && /*#__PURE__*/React__default.createElement(OpenChannelMobileMenu, {
    message: message,
    parentRef: mobileMenuRef,
    hideMenu: () => {
      setContextMenu(false);
    },
    showRemove: () => {
      setContextMenu(false);
      showRemove(true);
    },
    showEdit: () => {
      setContextMenu(false);
      showEdit(true);
    },
    copyToClipboard: () => {
      setContextMenu(false);
      copyToClipboard(message === null || message === void 0 ? void 0 : message.message);
    },
    resendMessage: () => {
      setContextMenu(false);
      resendMessage(message);
    }
  }));
}

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