import { b as _extends, _ as _objectSpread2 } from '../_rollupPluginBabelHelpers-a69fd1ce.js';
import React__default, { useRef, useContext, useState } from 'react';
import { f as format } from '../index-e7f4cbdf.js';
import Avatar from './Avatar.js';
import ContextMenu, { MenuItems, MenuItem } from './ContextMenu.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../index-4105bc4d.js';
import Loader from './Loader.js';
import Icon, { IconTypes, IconColors } from './Icon.js';
import IconButton from './IconButton.js';
import TextButton from './TextButton.js';
import UserProfile from './UserProfile.js';
import { a as UserProfileContext } from '../UserProfileContext-a59f0533.js';
import { u as useLocalization } from '../LocalizationContext-426f3d58.js';
import { g as getSenderFromMessage, b as isFineResend, s as showMenuTrigger, c as isFineDelete, O as OpenChannelMobileMenu, d as checkIsPending, e as checkIsFailed } from '../index-2528bdee.js';
import { u as useMediaQueryContext } from '../MediaQueryContext-b531adc9.js';
import { u as useLongPress } from '../useLongPress-c0ba590c.js';
import '../index-90420b99.js';
import './ImageRenderer.js';
import '../uuid-2e8efc75.js';
import 'prop-types';
import 'react-dom';
import './SortByRow.js';
import '../index-f83f0aac.js';
import '../utils/message/getOutgoingMessageState.js';
import '../stringSet-a62108b2.js';
import '../color-e708f53d.js';
import '../sendbirdSelectors.js';
import '../topics-247fcf3a.js';
import '../utils-4bf6e4e6.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';

const checkFileType = fileUrl => {
  const audioFile = /(\.mp3)$/i;
  const gifFile = /(\.gif)$/i;
  if (audioFile.test(fileUrl)) {
    return IconTypes.FILE_AUDIO;
  }
  if (gifFile.test(fileUrl)) {
    return IconTypes.GIF;
  }
  return IconTypes.FILE_DOCUMENT;
};
const truncate = (fullStr, strLen) => {
  if (fullStr === null || fullStr === undefined) return '';
  if (fullStr.length <= strLen) return fullStr;
  const separator = '...';
  const sepLen = separator.length;
  const charsToShow = strLen - sepLen;
  const frontChars = Math.ceil(charsToShow / 2);
  const backChars = Math.floor(charsToShow / 2);
  return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars);
};

function OpenchannelFileMessage(_ref) {
  let {
    className,
    message,
    isOperator,
    isEphemeral = false,
    userId,
    disabled,
    chainTop,
    showRemove,
    resendMessage
  } = _ref;
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const {
    dateLocale,
    stringSet
  } = useLocalization();
  const contextMenuRef = useRef(null);
  const mobileMenuRef = useRef(null);
  const avatarRef = useRef(null);
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(UserProfileContext);
  const {
    isMobile
  } = useMediaQueryContext();
  const openFileUrl = () => {
    window.open(message.url);
  };
  const isPending = checkIsPending(status);
  const isFailed = checkIsFailed(status);
  const sender = getSenderFromMessage(message);
  const [contextMenu, setContextMenu] = useState(false);
  const longPress = useLongPress({
    onLongPress: () => {
      if (isMobile) {
        setContextMenu(true);
      }
    },
    onClick: openFileUrl
  }, {
    delay: 300
  });
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", {
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-file-message'].join(' '),
    ref: mobileMenuRef
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-file-message__left"
  }, !chainTop && /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(Avatar, {
      className: "sendbird-openchannel-file-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-file-message__right"
  }, !chainTop && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-file-message__right__title"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-file-message__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-file-message__right__title__sent-at",
    type: LabelTypography.CAPTION_3,
    color: LabelColors.ONBACKGROUND_3
  }, (message === null || message === void 0 ? void 0 : message.createdAt) && format(message.createdAt, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default.createElement("div", _extends({
    className: "sendbird-openchannel-file-message__right__body"
  }, isMobile ? _objectSpread2({}, longPress) : {}), checkFileType(message.url) && /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-file-message__right__body__icon",
    type: checkFileType(message.url),
    fillColor: IconColors.PRIMARY,
    width: "48px",
    height: "48px"
  }), /*#__PURE__*/React__default.createElement(TextButton, {
    className: "sendbird-openchannel-file-message__right__body__file-name",
    onClick: openFileUrl
  }, /*#__PURE__*/React__default.createElement(Label, {
    type: LabelTypography.BODY_1,
    color: LabelColors.ONBACKGROUND_1
  }, truncate(message.name || message.url, 40)))), (isPending || isFailed) && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-file-message__right__tail"
  }, isPending && /*#__PURE__*/React__default.createElement(Loader, {
    width: "16px",
    height: "16px"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-file-message__right__tail__pending",
    type: IconTypes.SPINNER,
    fillColor: IconColors.PRIMARY,
    width: "16px",
    height: "16px"
  })), isFailed && /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-file-message__right__tail__failed",
    type: IconTypes.ERROR,
    fillColor: IconColors.ERROR,
    width: "16px",
    height: "16px"
  }))), !isMobile && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-file-message__context-menu",
    ref: contextMenuRef
  }, (isFineResend({
    message,
    userId,
    status
  }) || !isEphemeral) && /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => showMenuTrigger({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(IconButton, {
      className: "sendbird-openchannel-file-message__context-menu__icon",
      width: "32px",
      height: "32px",
      onClick: toggleDropdown
    }, /*#__PURE__*/React__default.createElement(Icon, {
      type: IconTypes.MORE,
      width: "24px",
      height: "24px"
    })),
    menuItems: closeDropdown => /*#__PURE__*/React__default.createElement(MenuItems, {
      parentRef: contextMenuRef,
      parentContainRef: contextMenuRef,
      closeDropdown: closeDropdown,
      openLeft: true
    }, isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      onClick: () => {
        if (disabled) {
          return;
        }
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_file_message_context_menu_resend"
    }, stringSet.CONTEXT_MENU_DROPDOWN__RESEND), !isEphemeral && isFineDelete({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      onClick: () => {
        if (disabled) {
          return;
        }
        showRemove(true);
        closeDropdown();
      },
      dataSbId: "open_channel_file_message_context_menu_delete"
    }, stringSet.CONTEXT_MENU_DROPDOWN__DELETE))
  }))), contextMenu && /*#__PURE__*/React__default.createElement(OpenChannelMobileMenu, {
    message: message,
    hideMenu: () => {
      setContextMenu(false);
    },
    parentRef: mobileMenuRef,
    showRemove: () => {
      setContextMenu(false);
      showRemove(true);
    }
  }));
}

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