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