import { b as _extends, _ as _objectSpread2 } from '../_rollupPluginBabelHelpers-52628376.js';
import React__default, { useContext, useState, useRef, useMemo, useEffect } from 'react';
import { f as format } from '../index-0e63045e.js';
import { u as useLocalization } from '../LocalizationContext-bcaf447a.js';
import Avatar from './Avatar.js';
import ContextMenu, { MenuItems, MenuItem } from './ContextMenu.js';
import Icon, { IconColors, IconTypes } from './Icon.js';
import IconButton from './IconButton.js';
import ImageRenderer from './ImageRenderer.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../index-208f60f9.js';
import Loader from './Loader.js';
import UserProfile from './UserProfile.js';
import { a as UserProfileContext } from '../UserProfileContext-96f68187.js';
import { g as getSenderFromMessage, b as isFineResend, s as showMenuTrigger, c as isFineDelete, O as OpenChannelMobileMenu, f as checkIsSent, d as checkIsPending, e as checkIsFailed } from '../index-c4826ede.js';
import { u as useMediaQueryContext } from '../MediaQueryContext-0d6b7936.js';
import { u as useLongPress } from '../useLongPress-8b46567a.js';
import '../index-26c66412.js';
import '../stringSet-7f300d56.js';
import '../uuid-9e3aeec6.js';
import 'prop-types';
import 'react-dom';
import './SortByRow.js';
import '../index-46da725e.js';
import '../utils/message/getOutgoingMessageState.js';
import '../sendbirdSelectors.js';
import '../topics-50ed8c29.js';
import '../utils-4ce9435a.js';
import './Button.js';
import '../useSendbirdStateContext.js';
import '../withSendbird.js';

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
  } = useLocalization();
  const {
    disableUserProfile,
    renderUserProfile
  } = useContext(UserProfileContext);
  const [messageWidth, setMessageWidth] = useState(360);
  const [contextMenu, setContextMenu] = useState(false);
  const messageRef = useRef(null);
  const mobileMenuRef = useRef(null);
  const contextMenuRef = useRef(null);
  const avatarRef = useRef(null);
  const onLongPress = useLongPress({
    onLongPress: () => {
      setContextMenu(true);
    },
    onClick: () => {
      onClick(true);
    }
  });
  const {
    isMobile
  } = useMediaQueryContext();
  const memorizedThumbnailPlaceHolder = useMemo(() => type => _ref2 => {
    let {
      style
    } = _ref2;
    return (
      /*#__PURE__*/
      // eslint-disable-line
      React__default.createElement("div", {
        style: style
      }, /*#__PURE__*/React__default.createElement(Icon, {
        type: type,
        fillColor: IconColors.ON_BACKGROUND_2,
        width: "56px",
        height: "56px"
      }))
    );
  }, []);
  const isMessageSent = checkIsSent(status);
  const isPending = checkIsPending(status);
  const isFailed = checkIsFailed(status);
  const sender = getSenderFromMessage(message);
  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.createElement(React__default.Fragment, null, /*#__PURE__*/React__default.createElement("div", {
    className: [...(Array.isArray(className) ? className : [className]), 'sendbird-openchannel-thumbnail-message'].join(' '),
    ref: messageRef
  }, /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__left"
  }, !chainTop && /*#__PURE__*/React__default.createElement(ContextMenu, {
    menuTrigger: toggleDropdown => /*#__PURE__*/React__default.createElement(Avatar, {
      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.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-thumbnail-message__right"
  }, !chainTop && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__right__title"
  }, /*#__PURE__*/React__default.createElement(Label, {
    className: "sendbird-openchannel-thumbnail-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-thumbnail-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", {
    className: "sendbird-openchannel-thumbnail-message__right__body",
    ref: mobileMenuRef
  }, /*#__PURE__*/React__default.createElement("div", _extends({
    className: "sendbird-openchannel-thumbnail-message__right__body__wrap",
    role: "button",
    onClick: () => {
      if (isMessageSent) {
        onClick(true);
      }
    },
    onKeyDown: () => {
      if (isMessageSent) {
        onClick(true);
      }
    },
    tabIndex: 0
  }, isMobile ? _objectSpread2({}, onLongPress) : {}), /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__right__body__wrap__overlay"
  }), {
    [SUPPORTING_TYPES.VIDEO]: url || localUrl ? /*#__PURE__*/React__default.createElement("div", {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video"
    }, thumbnailUrl ? /*#__PURE__*/React__default.createElement(ImageRenderer, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video",
      url: thumbnailUrl,
      width: messageWidth,
      height: "270px",
      alt: "image",
      placeHolder: memorizedThumbnailPlaceHolder(IconTypes.PLAY)
    }) : /*#__PURE__*/React__default.createElement("video", {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video__video"
    }, /*#__PURE__*/React__default.createElement("source", {
      src: url || localUrl,
      type: type
    })), /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video__icon",
      type: IconTypes.PLAY,
      fillColor: IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    })) : /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__video--icon",
      type: IconTypes.PHOTO,
      fillColor: IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    }),
    [SUPPORTING_TYPES.IMAGE]: url || localUrl ? /*#__PURE__*/React__default.createElement(ImageRenderer, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__image",
      url: thumbnailUrl || url || localUrl,
      alt: "image",
      width: messageWidth,
      height: "270px",
      placeHolder: memorizedThumbnailPlaceHolder(IconTypes.PHOTO)
    }) : /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__image--icon",
      type: IconTypes.PHOTO,
      fillColor: IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    }),
    [SUPPORTING_TYPES.UNSUPPORTED]: /*#__PURE__*/React__default.createElement(Icon, {
      className: "sendbird-openchannel-thumbnail-message__right__body__wrap__unknown",
      type: IconTypes.PHOTO,
      fillColor: IconColors.ON_BACKGROUND_2,
      width: "56px",
      height: "56px"
    })
  }[getSupportingFileType(type)])), (isPending || isFailed) && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-thumbnail-message__right__tail"
  }, isPending && /*#__PURE__*/React__default.createElement(Loader, {
    width: "16px",
    height: "16px"
  }, /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-thumbnail-message__right__tail__pending",
    type: IconTypes.SPINNER,
    fillColor: IconColors.PRIMARY,
    width: "16px",
    height: "16px"
  })), isFailed && /*#__PURE__*/React__default.createElement(Icon, {
    className: "sendbird-openchannel-thumbnail-message__right__tail__failed",
    type: IconTypes.ERROR,
    fillColor: IconColors.ERROR,
    width: "16px",
    height: "16px"
  }))), !isMobile && /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-openchannel-thumbnail-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-thumbnail-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
    }, isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default.createElement(MenuItem, {
      onClick: () => {
        resendMessage(message);
        closeDropdown();
      },
      dataSbId: "open_channel_thumbnail_message_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_thumbnail_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);
    },
    resendMessage: () => {
      setContextMenu(false);
      resendMessage(message);
    }
  }));
}

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