'use strict';

var React = require('react');
var index = require('../../index-849f44b0.js');
var ui_OpenchannelUserMessage = require('../../ui/OpenchannelUserMessage.js');
var ui_OpenChannelAdminMessage = require('../../ui/OpenChannelAdminMessage.js');
var ui_OpenchannelOGMessage = require('../../ui/OpenchannelOGMessage.js');
var ui_OpenchannelThumbnailMessage = require('../../ui/OpenchannelThumbnailMessage.js');
var ui_OpenchannelFileMessage = require('../../ui/OpenchannelFileMessage.js');
var ui_DateSeparator = require('../../ui/DateSeparator.js');
var ui_Label = require('../../index-72e412f7.js');
var ui_MessageInput = require('../../ui/MessageInput.js');
var ui_FileViewer = require('../../ui/FileViewer.js');
var ui_Modal = require('../../ui/Modal.js');
var LocalizationContext = require('../../LocalizationContext-b42fa07d.js');
var _rollupPluginBabelHelpers = require('../../_rollupPluginBabelHelpers-3a9d1641.js');
var OpenChannel_context = require('../../OpenChannelProvider-94eb45b4.js');
var useSendbirdStateContext = require('../../useSendbirdStateContext.js');
require('../../index-97005534.js');
require('../../ui/Avatar.js');
require('../../ui/ImageRenderer.js');
require('../../ui/Icon.js');
require('prop-types');
require('../../uuid-5ab4f88b.js');
require('../../ui/ContextMenu.js');
require('react-dom');
require('../../ui/SortByRow.js');
require('../../index-0b8a1074.js');
require('../../utils/message/getOutgoingMessageState.js');
require('../../stringSet-4f30313f.js');
require('../../ui/IconButton.js');
require('../../ui/Loader.js');
require('../../ui/UserProfile.js');
require('../../UserProfileContext-c87514d3.js');
require('../../sendbirdSelectors.js');
require('../../topics-3226c6ee.js');
require('../../utils-146517a9.js');
require('../../ui/Button.js');
require('../../utils-dbaff0d0.js');
require('../../index-f4f6d794.js');
require('../../MediaQueryContext-372b55a6.js');
require('../../useLongPress-bf1e8eee.js');
require('../../ui/LinkLabel.js');
require('../../index-1c18afa8.js');
require('../../index.module-d2876b1c.js');
require('../../tokenize-e944d548.js');
require('../../Message/context.js');
require('../../ui/MentionLabel.js');
require('../../ui/TextButton.js');
require('../../color-a4e73e6f.js');
require('../../const-3ab3e45c.js');
require('../../const-68cf2709.js');
require('dompurify');
require('../../consts-bb2a67c7.js');
require('../../consts-ad8ba629.js');
require('../../consts-26d4668c.js');
require('../../index-e8b83547.js');
require('../../compareIds-a7029d6a.js');
require('@sendbird/chat');
require('@sendbird/chat/openChannel');
require('../../withSendbird.js');

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

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

function RemoveMessageModal(_ref) {
  let {
    onCloseModal,
    onDeleteMessage
  } = _ref;
  const {
    stringSet
  } = React.useContext(LocalizationContext.LocalizationContext);
  return /*#__PURE__*/React__default["default"].createElement(ui_Modal["default"], {
    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;
_rollupPluginBabelHelpers._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 => {
  var _message$isUserMessag, _message$isAdminMessa;
  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 ? 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
  } = OpenChannel_context.useOpenChannelContext();
  const {
    dateLocale
  } = LocalizationContext.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;
  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 = React.useMemo(() => props => {
    return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, renderMessage ? renderMessage(props) : null);
  }, [message, renderMessage]);
  const [showEdit, setShowEdit] = React.useState(false);
  const [showRemove, setShowRemove] = React.useState(false);
  const [showFileViewer, setShowFileViewer] = React.useState(false);
  const editMessageInputRef = React.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["default"].createElement("div", {
      className: "sendbird-msg-hoc sendbird-msg--scroll-ref"
    }, /*#__PURE__*/React__default["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["default"].createElement(ui_MessageInput, {
      isEdit: true,
      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["default"].createElement("div", {
    className: "sendbird-msg-hoc sendbird-msg--scroll-ref"
  }, hasSeparator && (message === null || message === void 0 ? void 0 : message.createdAt) && /*#__PURE__*/React__default["default"].createElement(ui_DateSeparator, null, /*#__PURE__*/React__default["default"].createElement(ui_Label.Label, {
    type: ui_Label.LabelTypography.CAPTION_2,
    color: ui_Label.LabelColors.ONBACKGROUND_2
  }, index.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["default"].createElement(ui_OpenChannelAdminMessage, {
          message: message
        });
      }
    })(),
    [MessageTypes.FILE]: (() => {
      if ((message === null || message === void 0 ? void 0 : message.messageType) === 'file') {
        var _sender;
        return /*#__PURE__*/React__default["default"].createElement(ui_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') {
        var _sender2;
        return /*#__PURE__*/React__default["default"].createElement(ui_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["default"].createElement(ui_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["default"].createElement(ui_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)], showRemove && /*#__PURE__*/React__default["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["default"].createElement(ui_FileViewer["default"], {
    onClose: () => setShowFileViewer(false),
    message: message,
    onDelete: () => deleteMessage(message),
    isByMe: isByMe
  }));
}

module.exports = MessagOpenChannelMessageeHoc;
//# sourceMappingURL=OpenChannelMessage.js.map
