import React__default, { useContext, useMemo, useState, useRef } from 'react';
import { f as format } from '../../index-5a62f6b7.js';
import OpenchannelUserMessage from '../../ui/OpenchannelUserMessage.js';
import OpenChannelAdminMessage from '../../ui/OpenChannelAdminMessage.js';
import OpenchannelOGMessage from '../../ui/OpenchannelOGMessage.js';
import OpenchannelThumbnailMessage from '../../ui/OpenchannelThumbnailMessage.js';
import OpenchannelFileMessage from '../../ui/OpenchannelFileMessage.js';
import DateSeparator from '../../ui/DateSeparator.js';
import { L as Label, a as LabelTypography, b as LabelColors } from '../../index-f50d7ab0.js';
import MessageInput from '../../ui/MessageInput.js';
import FileViewer from '../../ui/FileViewer.js';
import Modal from '../../ui/Modal.js';
import { a as LocalizationContext, u as useLocalization } from '../../LocalizationContext-1701e5c8.js';
import { _ as _objectSpread2 } from '../../_rollupPluginBabelHelpers-cc035350.js';
import { u as useOpenChannelContext } from '../../OpenChannelProvider-995a7397.js';
import useSendbirdStateContext from '../../useSendbirdStateContext.js';
import '../../index-ff8f2f61.js';
import '../../ui/Avatar.js';
import '../../ui/ImageRenderer.js';
import '../../ui/Icon.js';
import 'prop-types';
import '../../uuid-8b344dd2.js';
import '../../ui/ContextMenu.js';
import 'react-dom';
import '../../ui/SortByRow.js';
import '../../index-99ab01f6.js';
import '../../utils/message/getOutgoingMessageState.js';
import '../../stringSet-e9bb0333.js';
import '../../ui/IconButton.js';
import '../../ui/Loader.js';
import '../../ui/UserProfile.js';
import '../../UserProfileContext-74e9a128.js';
import '../../sendbirdSelectors.js';
import '../../topics-0b30f52b.js';
import '../../utils-05aa9060.js';
import '../../ui/Button.js';
import '../../utils-a59107a2.js';
import '../../index-e30c58e0.js';
import '../../MediaQueryContext-e5b401d0.js';
import '../../useLongPress-6d0b6619.js';
import '../../ui/LinkLabel.js';
import '../../index-a31ee68d.js';
import '../../index.module-d24badee.js';
import '../../tokenize-d12690c3.js';
import '../../Message/context.js';
import '../../ui/MentionLabel.js';
import '../../ui/TextButton.js';
import '../../color-ea3bf5b8.js';
import '../../const-2e67461b.js';
import '../../const-7c266a95.js';
import 'dompurify';
import '../../consts-a0834b70.js';
import '../../consts-3a33a0a8.js';
import '../../consts-b3274b3d.js';
import '../../withSendbird.js';
import '../../index-44c86e58.js';
import '../../compareIds-367bb030.js';
import '@sendbird/chat';
import '@sendbird/chat/openChannel';

function RemoveMessageModal(_ref) {
  let {
    onCloseModal,
    onDeleteMessage
  } = _ref;
  const {
    stringSet
  } = useContext(LocalizationContext);
  return /*#__PURE__*/React__default.createElement(Modal, {
    onCancel: onCloseModal,
    onSubmit: onDeleteMessage,
    submitText: stringSet.MESSAGE_MENU__DELETE,
    titleText: stringSet.MODAL__DELETE_MESSAGE__TITLE
  });
}

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types

const SUPPORTED_MIMES = {
  IMAGE: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'image/svg+xml', 'image/webp'],
  VIDEO: ['video/mpeg', 'video/ogg', 'video/webm', 'video/mp4']
};
const isImage = type => SUPPORTED_MIMES.IMAGE.indexOf(type) >= 0;
const isVideo = type => SUPPORTED_MIMES.VIDEO.indexOf(type) >= 0;
_objectSpread2({}, SUPPORTED_MIMES);

const MessageTypes = {
  ADMIN: 'ADMIN',
  USER: 'USER',
  FILE: 'FILE',
  THUMBNAIL: 'THUMBNAIL',
  OG: 'OG',
  UNKNOWN: 'UNKNOWN'
};
const SendingMessageStatus = {
  NONE: 'none',
  SUCCEEDED: 'succeeded',
  FAILED: 'failed',
  PENDING: 'pending'
};
const getMessageType = (message, options) => {
  var _message$isUserMessag, _message$isAdminMessa;
  const isOgMessageEnabledInOpenChannel = options === null || options === void 0 ? void 0 : options.isOgMessageEnabledInOpenChannel;
  if (message !== null && message !== void 0 && (_message$isUserMessag = message.isUserMessage) !== null && _message$isUserMessag !== void 0 && _message$isUserMessag.call(message) || (message === null || message === void 0 ? void 0 : message.messageType) === 'user') {
    return message !== null && message !== void 0 && message.ogMetaData && isOgMessageEnabledInOpenChannel ? MessageTypes.OG : MessageTypes.USER;
  }
  if (message !== null && message !== void 0 && (_message$isAdminMessa = message.isAdminMessage) !== null && _message$isAdminMessa !== void 0 && _message$isAdminMessa.call(message)) {
    return MessageTypes.ADMIN;
  }
  if ((message === null || message === void 0 ? void 0 : message.messageType) === 'file') {
    return isImage(message.type) || isVideo(message.type) ? MessageTypes.THUMBNAIL : MessageTypes.FILE;
  }
  return MessageTypes.UNKNOWN;
};

function MessagOpenChannelMessageeHoc(props) {
  var _globalState$config;
  const {
    message,
    chainTop,
    chainBottom,
    hasSeparator,
    renderMessage
  } = props;
  const {
    currentOpenChannel,
    deleteMessage,
    updateMessage,
    resendMessage
  } = useOpenChannelContext();
  const {
    dateLocale
  } = useLocalization();
  const editDisabled = currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isFrozen;
  const globalState = useSendbirdStateContext();
  const userId = globalState === null || globalState === void 0 ? void 0 : (_globalState$config = globalState.config) === null || _globalState$config === void 0 ? void 0 : _globalState$config.userId;
  const isOgMessageEnabledInOpenChannel = globalState.config.openChannel.enableOgtag;
  let sender = null;
  if ((message === null || message === void 0 ? void 0 : message.messageType) !== 'admin') {
    sender = message === null || message === void 0 ? void 0 : message.sender;
  }
  const RenderedMessage = useMemo(() => props => {
    return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, renderMessage ? renderMessage(props) : null);
  }, [message, renderMessage]);
  const [showEdit, setShowEdit] = useState(false);
  const [showRemove, setShowRemove] = useState(false);
  const [showFileViewer, setShowFileViewer] = useState(false);
  const editMessageInputRef = useRef(null);
  let isByMe = false;
  if (sender && (message === null || message === void 0 ? void 0 : message.messageType) !== 'admin') {
    // pending and failed messages are by me
    isByMe = userId === sender.userId || (message === null || message === void 0 ? void 0 : message.sendingStatus) === SendingMessageStatus.PENDING || (message === null || message === void 0 ? void 0 : message.sendingStatus) === SendingMessageStatus.FAILED;
  }
  if (renderMessage && RenderedMessage) {
    return /*#__PURE__*/React__default.createElement("div", {
      className: "sendbird-msg-hoc sendbird-msg--scroll-ref"
    }, /*#__PURE__*/React__default.createElement(RenderedMessage, {
      message: message,
      chainTop: chainTop,
      chainBottom: chainBottom
    }));
  }
  if ((message === null || message === void 0 ? void 0 : message.messageType) === 'user' && showEdit) {
    return /*#__PURE__*/React__default.createElement(MessageInput, {
      isEdit: true,
      channel: currentOpenChannel,
      disabled: editDisabled,
      ref: editMessageInputRef,
      message: message,
      name: message === null || message === void 0 ? void 0 : message.messageId,
      onUpdateMessage: _ref => {
        let {
          messageId,
          message
        } = _ref;
        updateMessage(messageId, message);
        setShowEdit(false);
      },
      onCancelEdit: () => {
        setShowEdit(false);
      }
    });
  }
  return /*#__PURE__*/React__default.createElement("div", {
    className: "sendbird-msg-hoc sendbird-msg--scroll-ref"
  }, hasSeparator && (message === null || message === void 0 ? void 0 : message.createdAt) && /*#__PURE__*/React__default.createElement(DateSeparator, null, /*#__PURE__*/React__default.createElement(Label, {
    type: LabelTypography.CAPTION_2,
    color: LabelColors.ONBACKGROUND_2
  }, format(message === null || message === void 0 ? void 0 : message.createdAt, 'MMMM dd, yyyy', {
    locale: dateLocale
  }))), {
    [MessageTypes.ADMIN]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'admin') {
        return /*#__PURE__*/React__default.createElement(OpenChannelAdminMessage, {
          message: message
        });
      }
    })(),
    [MessageTypes.FILE]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'file') {
        var _sender;
        return /*#__PURE__*/React__default.createElement(OpenchannelFileMessage, {
          message: message,
          isOperator: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isOperator(message === null || message === void 0 ? void 0 : (_sender = message.sender) === null || _sender === void 0 ? void 0 : _sender.userId),
          isEphemeral: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isEphemeral,
          disabled: editDisabled,
          userId: userId,
          showRemove: setShowRemove,
          resendMessage: resendMessage,
          chainTop: chainTop,
          chainBottom: chainBottom
        });
      }
    })(),
    [MessageTypes.OG]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'user' && isOgMessageEnabledInOpenChannel) {
        var _sender2;
        return /*#__PURE__*/React__default.createElement(OpenchannelOGMessage, {
          message: message,
          isOperator: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isOperator(message === null || message === void 0 ? void 0 : (_sender2 = message.sender) === null || _sender2 === void 0 ? void 0 : _sender2.userId),
          isEphemeral: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isEphemeral,
          userId: userId,
          showEdit: setShowEdit,
          disabled: editDisabled,
          showRemove: setShowRemove,
          resendMessage: resendMessage,
          chainTop: chainTop,
          chainBottom: chainBottom
        });
      }
    })(),
    [MessageTypes.THUMBNAIL]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'file') {
        var _sender3;
        return /*#__PURE__*/React__default.createElement(OpenchannelThumbnailMessage, {
          message: message,
          isOperator: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isOperator(message === null || message === void 0 ? void 0 : (_sender3 = message.sender) === null || _sender3 === void 0 ? void 0 : _sender3.userId),
          isEphemeral: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isEphemeral,
          disabled: editDisabled,
          userId: userId,
          showRemove: setShowRemove,
          resendMessage: resendMessage,
          onClick: setShowFileViewer,
          chainTop: chainTop,
          chainBottom: chainBottom
        });
      }
    })(),
    [MessageTypes.USER]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'user') {
        var _sender4;
        return /*#__PURE__*/React__default.createElement(OpenchannelUserMessage, {
          message: message,
          isOperator: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isOperator(message === null || message === void 0 ? void 0 : (_sender4 = message.sender) === null || _sender4 === void 0 ? void 0 : _sender4.userId),
          isEphemeral: currentOpenChannel === null || currentOpenChannel === void 0 ? void 0 : currentOpenChannel.isEphemeral,
          userId: userId,
          disabled: editDisabled,
          showEdit: setShowEdit,
          showRemove: setShowRemove,
          resendMessage: resendMessage,
          chainTop: chainTop,
          chainBottom: chainBottom
        });
      }
    })(),
    [MessageTypes.UNKNOWN]: (() => {
      // return (
      //   <OpenChannelUnknownMessage message={message} />
      // );
    })()
  }[getMessageType(message, {
    isOgMessageEnabledInOpenChannel
  })], showRemove && /*#__PURE__*/React__default.createElement(RemoveMessageModal, {
    onCloseModal: () => setShowRemove(false),
    onDeleteMessage: () => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) !== 'admin') {
        deleteMessage(message);
      }
    }
  }), showFileViewer && (message === null || message === void 0 ? void 0 : message.messageType) === 'file' && /*#__PURE__*/React__default.createElement(FileViewer, {
    onClose: () => setShowFileViewer(false),
    message: message,
    onDelete: () => deleteMessage(message),
    isByMe: isByMe
  }));
}

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