'use strict';

var _rollupPluginBabelHelpers = require('../_rollupPluginBabelHelpers-21ae158b.js');
var React = require('react');
var index$1 = require('../index-892b4b7f.js');
var LocalizationContext = require('../LocalizationContext-44d2f61b.js');
var ui_Avatar = require('./Avatar.js');
var ui_ContextMenu = require('./ContextMenu.js');
var ui_Icon = require('./Icon.js');
var ui_IconButton = require('./IconButton.js');
var ui_ImageRenderer = require('./ImageRenderer.js');
var ui_Label = require('../index-2bfd366f.js');
var ui_Loader = require('./Loader.js');
var ui_UserProfile = require('./UserProfile.js');
var UserProfileContext = require('../UserProfileContext-9d843d68.js');
var index = require('../index-b339cfde.js');
var MediaQueryContext = require('../MediaQueryContext-5d311a86.js');
var useLongPress = require('../useLongPress-83b6a2f1.js');
require('../index-88e8fdfa.js');
require('../stringSet-7aebf034.js');
require('../uuid-dee74248.js');
require('prop-types');
require('react-dom');
require('./SortByRow.js');
require('../index-1b3c69da.js');
require('../utils/message/getOutgoingMessageState.js');
require('../sendbirdSelectors.js');
require('../topics-373d2156.js');
require('../utils-6a748c02.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 SUPPORTING_TYPES = {
  IMAGE: 'IMAGE',
  VIDEO: 'VIDEO',
  UNSUPPORTED: 'UNSUPPORTED'
};
const SUPPORTED_MIMES = {
  IMAGE: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/webp'],
  VIDEO: ['video/mpeg', 'video/ogg', 'video/webm', 'video/mp4']
};
const getSupportingFileType = type => {
  if (SUPPORTED_MIMES.IMAGE.indexOf(type) >= 0) {
    return SUPPORTING_TYPES.IMAGE;
  }
  if (SUPPORTED_MIMES.VIDEO.indexOf(type) >= 0) {
    return SUPPORTING_TYPES.VIDEO;
  }
  return SUPPORTING_TYPES.UNSUPPORTED;
};

function OpenchannelThumbnailMessage(_ref) {
  let {
    className,
    message,
    isOperator,
    isEphemeral = false,
    disabled,
    userId,
    chainTop,
    onClick,
    showRemove,
    resendMessage
  } = _ref;
  const {
    type,
    url,
    thumbnails,
    localUrl
  } = message;
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const thumbnailUrl = thumbnails && thumbnails.length > 0 && thumbnails[0].url || null;
  const {
    stringSet,
    dateLocale
  } = LocalizationContext.useLocalization();
  const {
    disableUserProfile,
    renderUserProfile
  } = React.useContext(UserProfileContext.UserProfileContext);
  const [messageWidth, setMessageWidth] = React.useState(360);
  const [contextMenu, setContextMenu] = React.useState(false);
  const messageRef = React.useRef(null);
  const mobileMenuRef = React.useRef(null);
  const contextMenuRef = React.useRef(null);
  const avatarRef = React.useRef(null);
  const onLongPress = useLongPress.useLongPress({
    onLongPress: () => {
      setContextMenu(true);
    },
    onClick: () => {
      onClick(true);
    }
  });
  const {
    isMobile
  } = MediaQueryContext.useMediaQueryContext();
  const memorizedThumbnailPlaceHolder = React.useMemo(() => type => _ref2 => {
    let {
      style
    } = _ref2;
    return (
      /*#__PURE__*/
      // eslint-disable-line
      React__default["default"].createElement("div", {
        style: style
      }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
        type: type,
        fillColor: ui_Icon.IconColors.ON_BACKGROUND_2,
        width: "56px",
        height: "56px"
      }))
    );
  }, []);
  const isMessageSent = index.checkIsSent(status);
  const isPending = index.checkIsPending(status);
  const isFailed = index.checkIsFailed(status);
  const sender = index.getSenderFromMessage(message);
  React.useEffect(() => {
    var _messageRef$current$c, _messageRef$current;
    const thumbnailWidth = ((_messageRef$current$c = messageRef === null || messageRef === void 0 ? void 0 : (_messageRef$current = messageRef.current) === null || _messageRef$current === void 0 ? void 0 : _messageRef$current.clientWidth) !== null && _messageRef$current$c !== void 0 ? _messageRef$current$c : 0) - 80;
    setMessageWidth(thumbnailWidth > 360 ? 360 : thumbnailWidth);
  }, []);
  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-thumbnail-message'].join(' '),
    ref: messageRef
  }, /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__left"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default["default"].createElement(ui_Avatar["default"], {
      className: "sendbird-openchannel-thumbnail-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-thumbnail-message__right"
  }, !chainTop && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__right__title"
  }, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    className: "sendbird-openchannel-thumbnail-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-thumbnail-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", {
    className: "sendbird-openchannel-thumbnail-message__right__body",
    ref: mobileMenuRef
  }, /*#__PURE__*/React__default["default"].createElement("div", _rollupPluginBabelHelpers._extends({
    className: "sendbird-openchannel-thumbnail-message__right__body__wrap",
    role: "button",
    onClick: () => {
      if (isMessageSent) {
        onClick(true);
      }
    },
    onKeyDown: () => {
      if (isMessageSent) {
        onClick(true);
      }
    },
    tabIndex: 0
  }, isMobile ? _rollupPluginBabelHelpers._objectSpread2({}, onLongPress) : {}), /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__right__body__wrap__overlay"
  }), {
    [SUPPORTING_TYPES.VIDEO]: url || localUrl ? /*#__PURE__*/React__default["default"].createElement("div", {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video"
    }, thumbnailUrl ? /*#__PURE__*/React__default["default"].createElement(ui_ImageRenderer, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video",
      url: thumbnailUrl,
      width: messageWidth,
      height: "270px",
      alt: "image",
      placeHolder: memorizedThumbnailPlaceHolder(ui_Icon.IconTypes.PLAY)
    }) : /*#__PURE__*/React__default["default"].createElement("video", {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video__video"
    }, /*#__PURE__*/React__default["default"].createElement("source", {
      src: url || localUrl,
      type: type
    })), /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video__icon",
      type: ui_Icon.IconTypes.PLAY,
      fillColor: ui_Icon.IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    })) : /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video--icon",
      type: ui_Icon.IconTypes.PHOTO,
      fillColor: ui_Icon.IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    }),
    [SUPPORTING_TYPES.IMAGE]: url || localUrl ? /*#__PURE__*/React__default["default"].createElement(ui_ImageRenderer, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__image",
      url: thumbnailUrl || url || localUrl,
      alt: "image",
      width: messageWidth,
      height: "270px",
      placeHolder: memorizedThumbnailPlaceHolder(ui_Icon.IconTypes.PHOTO)
    }) : /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__image--icon",
      type: ui_Icon.IconTypes.PHOTO,
      fillColor: ui_Icon.IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    }),
    [SUPPORTING_TYPES.UNSUPPORTED]: /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__unknown",
      type: ui_Icon.IconTypes.PHOTO,
      fillColor: ui_Icon.IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    })
  }[getSupportingFileType(type)])), (isPending || isFailed) && /*#__PURE__*/React__default["default"].createElement("div", {
    className: "sendbird-openchannel-thumbnail-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-thumbnail-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-thumbnail-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-thumbnail-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-thumbnail-message__context-menu--icon",
      width: "32px",
      height: "32px",
      onClick: toggleDropdown
    }, /*#__PURE__*/React__default["default"].createElement(ui_Icon["default"], {
      type: ui_Icon.IconTypes.MORE,
      fillColor: ui_Icon.IconColors.CONTENT_INVERSE,
      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: () => {
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_thumbnail_message_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_thumbnail_message_menu_delete"
    }, stringSet.CONTEXT_MENU_DROPDOWN__DELETE))
  }))), contextMenu && /*#__PURE__*/React__default["default"].createElement(index.OpenChannelMobileMenu, {
    message: message,
    parentRef: mobileMenuRef,
    hideMenu: () => {
      setContextMenu(false);
    },
    showRemove: () => {
      setContextMenu(false);
      showRemove(true);
    },
    resendMessage: () => {
      setContextMenu(false);
      resendMessage(message);
    }
  }));
}

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