'use strict';

var React = require('react');
var ui_ContextMenu = require('./ui/ContextMenu.js');
var useSendbirdStateContext = require('./useSendbirdStateContext.js');
var LocalizationContext = require('./LocalizationContext-44d2f61b.js');

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

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

const OpenChannelMessageStatusTypes = {
  NONE: 'none',
  PENDING: 'pending',
  FAILED: 'failed',
  CANCELED: 'canceled',
  SUCCEEDED: 'succeeded'
};
const getSenderFromMessage = message => {
  // @ts-ignore
  return message.sender || message._sender;
};
const checkIsSent = status => status === OpenChannelMessageStatusTypes.SUCCEEDED;
const checkIsPending = status => status === OpenChannelMessageStatusTypes.PENDING;
const checkIsFailed = status => status === OpenChannelMessageStatusTypes.FAILED;
const checkIsByMe = (message, userId) => getSenderFromMessage(message).userId === userId;
const isFineCopy = _ref => {
  var _message$message;
  let {
    message
  } = _ref;
  return (message === null || message === void 0 ? void 0 : message.messageType) === 'user' && (message === null || message === void 0 ? void 0 : (_message$message = message.message) === null || _message$message === void 0 ? void 0 : _message$message.length) > 0;
};
const isFineResend = _ref2 => {
  let {
    message,
    status,
    userId
  } = _ref2;
  return checkIsByMe(message, userId) && checkIsFailed(status)
  // @ts-ignore
  && (message === null || message === void 0 ? void 0 : message.isResendable());
};
const isFineEdit = _ref3 => {
  var _message$isUserMessag;
  let {
    message,
    status,
    userId
  } = _ref3;
  return checkIsByMe(message, userId) && checkIsSent(status) && (message === null || message === void 0 ? void 0 : (_message$isUserMessag = message.isUserMessage) === null || _message$isUserMessag === void 0 ? void 0 : _message$isUserMessag.call(message));
};
const isFineDelete = _ref4 => {
  let {
    message,
    userId
  } = _ref4;
  return checkIsByMe(message, userId);
};
const isFineDownload = _ref5 => {
  var _message$isFileMessag;
  let {
    message,
    status
  } = _ref5;
  if (message !== null && message !== void 0 && (_message$isFileMessag = message.isFileMessage) !== null && _message$isFileMessag !== void 0 && _message$isFileMessag.call(message) && checkIsSent(status)) {
    return true;
  }
  return false;
};
const showMenuTrigger = props => {
  const {
    message,
    status,
    userId
  } = props;
  // @ts-ignore
  if (message.messageType === 'user') {
    return isFineDelete({
      message,
      status,
      userId
    }) || isFineEdit({
      message,
      status,
      userId
    })
    // @ts-ignore
    || isFineCopy({
      message,
      status,
      userId
    }) || isFineResend({
      message,
      status,
      userId
    });
  } else {
    return isFineDelete({
      message,
      status,
      userId
    }) || isFineResend({
      message,
      status,
      userId
    });
  }
};

const OpenChannelMobileMenu = props => {
  var _useSendbirdStateCont, _useSendbirdStateCont2;
  const {
    message,
    parentRef,
    resendMessage,
    showEdit,
    showRemove,
    copyToClipboard,
    hideMenu,
    isEphemeral = false
  } = props;
  const userMessage = message;
  const status = message === null || message === void 0 ? void 0 : message.sendingStatus;
  const {
    stringSet
  } = LocalizationContext.useLocalization();
  const userId = (_useSendbirdStateCont = useSendbirdStateContext()) === null || _useSendbirdStateCont === void 0 ? void 0 : (_useSendbirdStateCont2 = _useSendbirdStateCont.config) === null || _useSendbirdStateCont2 === void 0 ? void 0 : _useSendbirdStateCont2.userId;
  const fileMessage = message;
  return /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu["default"], {
    isOpen: true,
    menuItems: () => /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItems, {
      className: "sendbird-openchannel__mobile-menu",
      parentRef: parentRef,
      parentContainRef: parentRef,
      closeDropdown: hideMenu
    }, isFineCopy({
      message: userMessage,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__copy",
      onClick: () => {
        copyToClipboard();
      },
      dataSbId: "open_channel_mobile_context_menu_copy"
    }, /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, stringSet.CONTEXT_MENU_DROPDOWN__COPY)), !isEphemeral && isFineEdit({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      className: "sendbird-openchannel-og-message__top__context-menu__edit",
      onClick: () => {
        showEdit();
      },
      dataSbId: "open_channel_mobile_context_menu_edit"
    }, /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, stringSet.CONTEXT_MENU_DROPDOWN__EDIT)), isFineResend({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      onClick: () => {
        resendMessage();
      },
      dataSbId: "open_channel_mobile_context_menu_resend"
    }, /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, stringSet.CONTEXT_MENU_DROPDOWN__RESEND)), !isEphemeral && isFineDelete({
      message,
      userId,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      onClick: () => {
        showRemove();
      },
      dataSbId: "open_channel_mobile_context_menu_delete"
    }, /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, stringSet.CONTEXT_MENU_DROPDOWN__DELETE)), isFineDownload({
      message,
      status
    }) && /*#__PURE__*/React__default["default"].createElement(ui_ContextMenu.MenuItem, {
      onClick: () => {
        hideMenu();
      },
      dataSbId: "open_channel_mobile_context_menu_download_file"
    }, /*#__PURE__*/React__default["default"].createElement("a", {
      className: "sendbird-openchannel__mobile-menu-hyperlink",
      rel: "noopener noreferrer",
      href: fileMessage === null || fileMessage === void 0 ? void 0 : fileMessage.url,
      target: "_blank"
    }, stringSet.CONTEXT_MENU_DROPDOWN__SAVE)))
  });
};

exports.OpenChannelMobileMenu = OpenChannelMobileMenu;
exports.checkIsFailed = checkIsFailed;
exports.checkIsPending = checkIsPending;
exports.checkIsSent = checkIsSent;
exports.getSenderFromMessage = getSenderFromMessage;
exports.isFineCopy = isFineCopy;
exports.isFineDelete = isFineDelete;
exports.isFineEdit = isFineEdit;
exports.isFineResend = isFineResend;
exports.showMenuTrigger = showMenuTrigger;
//# sourceMappingURL=index-b339cfde.js.map
