import { b as _extends, _ as _objectSpread2 } from '../_rollupPluginBabelHelpers-cc035350.js';
import React__default, { useContext, useRef, useState, useEffect } from 'react';
import { f as format } from '../index-5a62f6b7.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 { L as Label, a as LabelTypography, b as LabelColors } from '../index-f50d7ab0.js';
import Loader from './Loader.js';
import UserProfile from './UserProfile.js';
import { a as UserProfileContext } from '../UserProfileContext-74e9a128.js';
import { u as useLocalization } from '../LocalizationContext-1701e5c8.js';
import { c as copyToClipboard } from '../utils-a59107a2.js';
import { u as uuidv4 } from '../uuid-8b344dd2.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-e30c58e0.js';
import { u as useMediaQueryContext } from '../MediaQueryContext-e5b401d0.js';
import { u as useLongPress } from '../useLongPress-6d0b6619.js';
import { B as isEditedMessage } from '../index-99ab01f6.js';
import '../index-ff8f2f61.js';
import './ImageRenderer.js';
import 'prop-types';
import 'react-dom';
import './SortByRow.js';
import '../utils/message/getOutgoingMessageState.js';
import '../stringSet-e9bb0333.js';
import '../sendbirdSelectors.js';
import '../topics-0b30f52b.js';
import '../utils-05aa9060.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';

function OpenchannelUserMessage(_ref) {
  let {
    className,
    message,
    isOperator,
    isEphemeral = false,
    userId,
    resendMessage,
    disabled,
    showEdit,
    showRemove,
    chainTop
  } = _ref;
  // hooks
  const {
    stringSet,
    dateLocale
  } = useLocalization();
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(UserProfileContext);
  const messageRef = useRef(null);
  const avatarRef = useRef(null);
  const contextMenuRef = useRef(null);
  const mobileMenuRef = useRef(null);
  const [contextStyle, setContextStyle] = useState({});
  const [contextMenu, setContextMenu] = useState(false);

  // consts
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const isPending = checkIsPending(status);
  const isFailed = checkIsFailed(status);
  const sender = getSenderFromMessage(message);

  // place context menu top depending clientHeight of message component
  useEffect(() => {
    var _messageRef$current;
    if ((messageRef === null || messageRef === void 0 ? void 0 : (_messageRef$current = messageRef.current) === null || _messageRef$current === void 0 ? void 0 : _messageRef$current.clientHeight) > 36) {
      setContextStyle({
        top: '8px '
      });
    } else {
      setContextStyle({
        top: '2px'
      });
    }
  }, [window.innerWidth]);
  const onLongPress = useLongPress({
    onLongPress: () => {
      setContextMenu(true);
    }
  });
  const {
    isMobile
  } = useMediaQueryContext();
  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", {
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-user-message'].join(' '),
    ref: messageRef
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-user-message__left"
  }, !chainTop && /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(Avatar, {
      className: "sendbird-openchannel-user-message__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-user-message__right"
  }, !chainTop && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-user-message__right__top"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-user-message__right__top__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-user-message__right__top__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", _extends({}, isMobile ? _objectSpread2({}, onLongPress) : {}, {
    className: "sendbird-openchannel-user-message__right__bottom",
    ref: mobileMenuRef
  }), /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-user-message__right__bottom__message",
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_1
  }, message === null || message === void 0 ? void 0 : message.message, isEditedMessage(message) && /*#__PURE__*/React__default.createElement(Label, {
    key: uuidv4(),
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_2,
    calssName: "sendbird-openchannel-user-message-word"
  }, ` ${stringSet.MESSAGE_EDITED} `))), (isPending || isFailed) && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-user-message__right__tail"
  }, isPending && /*#__PURE__*/React__default.createElement(Loader, {
    width: "16px",
    height: "16px"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-user-message__right__tail__pending",
    type: IconTypes.SPINNER,
    fillColor: IconColors.PRIMARY,
    width: "16px",
    height: "16px"
  })), isFailed && /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-user-message__right__tail__failed",
    type: IconTypes.ERROR,
    fillColor: IconColors.ERROR,
    width: "16px",
    height: "16px"
  }))), !isMobile && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-user-message__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-user-message__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: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      className: "sendbird-openchannel-user-message__context-menu__copy",
      onClick: () => {
        copyToClipboard(message.message);
        closeDropdown();
      },
      dataSbId: "open_channel_user_message_menu_copy"
    }, stringSet.CONTEXT_MENU_DROPDOWN__COPY), !isEphemeral && isFineEdit({
      message: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      className: "sendbird-openchannel-user-message__context-menu__edit",
      onClick: () => {
        if (disabled) {
          return;
        }
        showEdit(true);
        closeDropdown();
      },
      dataSbId: "open_channel_user_message_menu_edit"
    }, stringSet.CONTEXT_MENU_DROPDOWN__EDIT), isFineResend({
      message: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      className: "sendbird-openchannel-user-message__context-menu__resend",
      onClick: () => {
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_user_message_menu_resend"
    }, stringSet.CONTEXT_MENU_DROPDOWN__RESEND), !isEphemeral && isFineDelete({
      message: message,
      userId: userId,
      status: status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      className: "sendbird-openchannel-user-message__context-menu__delete",
      onClick: () => {
        if (disabled) {
          return;
        }
        showRemove(true);
        closeDropdown();
      },
      dataSbId: "open_channel_user_message_menu_delete"
    }, stringSet.CONTEXT_MENU_DROPDOWN__DELETE))
  }))), 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 { OpenchannelUserMessage as default };
//# sourceMappingURL=OpenchannelUserMessage.js.map
