'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_Label = require('../index-bff918b2.js');
var ui_Loader = require('./Loader.js');
var ui_Icon = require('./Icon.js');
var ui_IconButton = require('./IconButton.js');
var ui_TextButton = require('./TextButton.js');
var ui_UserProfile = require('./UserProfile.js');
var UserProfileContext = require('../UserProfileContext-2ff8462c.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');
require('../index-6a7f94de.js');
require('./ImageRenderer.js');
require('../uuid-f1d9be11.js');
require('prop-types');
require('react-dom');
require('./SortByRow.js');
require('../index-86e680cb.js');
require('../utils/message/getOutgoingMessageState.js');
require('../stringSet-5157206d.js');
require('../color-6641e39b.js');
require('../sendbirdSelectors.js');
require('../topics-2fe6fef6.js');
require('../utils-58f8eea0.js');
require('./Button.js');
require('../useSendbirdStateContext.js');
require('../withSendbird.js');

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

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

const checkFileType = fileUrl => {
  const audioFile = /(\.mp3)$/i;
  const gifFile = /(\.gif)$/i;
  if (audioFile.test(fileUrl)) {
    return ui_Icon.IconTypes.FILE_AUDIO;
  }
  if (gifFile.test(fileUrl)) {
    return ui_Icon.IconTypes.GIF;
  }
  return ui_Icon.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
  } = LocalizationContext.useLocalization();
  const contextMenuRef = React.useRef(null);
  const mobileMenuRef = React.useRef(null);
  const avatarRef = React.useRef(null);
  const {
    disableUserProfile,
    renderUserProfile
  } = React.useContext(UserProfileContext.UserProfileContext);
  const {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const openFileUrl = () => {
    window.open(message.url);
  };
  const isPending = index.checkIsPending(status);
  const isFailed = index.checkIsFailed(status);
  const sender = index.getSenderFromMessage(message);
  const [contextMenu, setContextMenu] = React.useState(false);
  const longPress = useLongPress.useLongPress({
    onLongPress: () => {
      if (isMobile) {
        setContextMenu(true);
      }
    },
    onClick: openFileUrl
  }, {
    delay: 300
  });
  return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, /*#__PURE__*/React__default["default"].createElement("div", {
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-file-message'].join(' '),
    ref: mobileMenuRef
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-file-message__left"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
      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["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-file-message__right"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-file-message__right__title"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-file-message__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-file-message__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.createdAt, 'p', {
    locale: dateLocale
  }))), /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers._extends({
    className: "sendbird-openchannel-file-message__right__body"
  }, isMobile ? _rollupPluginBabelHelpers._objectSpread2({}, longPress) : {}), checkFileType(message.url) && /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
    className: "sendbird-openchannel-file-message__right__body__icon",
    type: checkFileType(message.url),
    fillColor: ui_Icon.IconColors.PRIMARY,
    width: "48px",
    height: "48px"
  }), /*#__PURE__*/React__default["default"].createElement(ui_TextButton, {
    className: "sendbird-openchannel-file-message__right__body__file-name",
    onClick: openFileUrl
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    type: ui_Label.LabelTypography.BODY_1,
    color: ui_Label.LabelColors.ONBACKGROUND_1
  }, truncate(message.name || message.url, 40)))), (isPending || isFailed) && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-file-message__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-file-message__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-file-message__right__tail__failed",
    type: ui_Icon.IconTypes.ERROR,
    fillColor: ui_Icon.IconColors.ERROR,
    width: "16px",
    height: "16px"
  }))), !isMobile && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-file-message__context-menu",
    ref: contextMenuRef
  }, (index.isFineResend({
    message,
    userId,
    status
  }) || !isEphemeral) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => index.showMenuTrigger({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_IconButton, {
      className: "sendbird-openchannel-file-message__context-menu__icon",
      width: "32px",
      height: "32px",
      onClick: toggleDropdown
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      type: ui_Icon.IconTypes.MORE,
      width: "24px",
      height: "24px"
    })),
    menuItems: closeDropdown => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      parentRef: contextMenuRef,
      parentContainRef: contextMenuRef,
      closeDropdown: closeDropdown,
      openLeft: true
    }, index.isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      onClick: () => {
        if (disabled) {
          return;
        }
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_file_message_context_menu_resend"
    }, stringSet.CONTEXT_MENU_DROPDOWN__RESEND), !isEphemeral && index.isFineDelete({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.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["default"].createElement(index.OpenChannelMobileMenu, {
    message: message,
    hideMenu: () => {
      setContextMenu(false);
    },
    parentRef: mobileMenuRef,
    showRemove: () => {
      setContextMenu(false);
      showRemove(true);
    }
  }));
}

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