"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Message = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactRedux = require("react-redux");
var _Avatar = require("./Avatar");
var _MessageInteractions = _interopRequireDefault(require("./MessageInteractions"));
var _BottomReplyContainer = require("./BottomReplyContainer");
var _MessageReply = require("./MessageReply");
var _DeletedMessage = require("./DeletedMessage");
var _chatSettingsSlice = require("../../roomStore/chatSettingsSlice");
var _MODAL_TYPES = require("../../helpers/constants/MODAL_TYPES");
var _roomsSlice = require("../../roomStore/roomsSlice");
var _native = _interopRequireDefault(require("styled-components/native"));
var _MediaMessage = _interopRequireDefault(require("../MainComponents/MediaMessage"));
var _MessageTranslations = _interopRequireDefault(require("./MessageTranslations"));
var _useChatSettingState = require("../../hooks/useChatSettingState");
var _parseMessageBody = require("../../helpers/parseMessageBody");
var _useMessageHeapState = require("../../hooks/useMessageHeapState");
var _icons = require("../../assets/icons");
var _xmppProvider = require("../../context/xmppProvider");
var _MessageReaction = require("./MessageReaction");
var _StyledComponents = require("../styled/StyledComponents");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const CustomMessageContainer = _native.default.View`
  flex-direction: row;
  padding: 10px;
  align-items: flex-end;
  justify-content: ${({
  isUser
}) => isUser ? 'flex-end' : 'flex-start'};
  margin-bottom: ${props => props.reply ? '20px' : '0px'};
`;
const CustomMessageBubble = _native.default.View`
  position: relative;
  max-width: 85%;
  min-width: 30%;
  padding: 10px;
  margin-right: ${({
  isUser
}) => isUser ? '0' : '10px'};
  margin-left: ${({
  isUser
}) => isUser ? '10px' : '0'};
  border-radius: 10px;
  border-bottom-left-radius: ${({
  isUser
}) => isUser ? '10' : '0'}px;
  border-bottom-right-radius: ${({
  isUser
}) => isUser ? '0' : '10'}px;
  background-color: ${({
  isUser,
  deleted,
  backgroundMessageUser,
  backgroundMessage
}) => deleted ? '#f5f5f5' : isUser ? backgroundMessageUser || '#d1e7ff' : backgroundMessage || '#fff'};
`;
const CustomMessageText = _native.default.Text`
  font-size: 16px;
  color: ${({
  color,
  colorUser,
  isUser
}) => isUser ? colorUser || '#333' : color || '#333'};
`;
const CustomMessagePhoto = _native.default.Image`
  width: 40px;
  height: 40px;
  border-radius: 20px;
`;
const CustomMessagePhotoContainer = _native.default.TouchableOpacity`
  margin-right: 10px;
`;
const CustomUserName = _native.default.Text`
  font-size: 14px;
  font-weight: 500;
  color: ${({
  color
}) => color || '#333'};
`;
const CustomMessageTimestamp = _native.default.Text`
  font-size: 12px;
  color: #999;
  color: ${({
  isUser,
  color,
  colorUser
}) => isUser ? colorUser || '#999' : color || '#999'};
  margin-top: 5px;
  align-self: flex-end;
`;
const Message = ({
  message,
  isUser,
  isReply
}) => {
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const {
    config,
    langSource,
    user
  } = (0, _useChatSettingState.useChatSettingState)();
  const {
    idSet
  } = (0, _useMessageHeapState.useMessageHeapState)();
  const [isPressed, setIsPressed] = (0, _react.useState)(false);
  if (__DEV__ && message.id && !globalThis.__loggedMsg?.[message.id]) {
    (globalThis.__loggedMsg ||= {})[message.id] = true;
  }
  const [contextMenuPosition, setContextMenuPosition] = (0, _react.useState)(null);
  const timerRef = (0, _react.useRef)(null);
  const messageRef = (0, _react.useRef)(null);
  const handleUserAvatarClick = user => {
    dispatch((0, _chatSettingsSlice.setActiveModal)(_MODAL_TYPES.MODAL_TYPES.PROFILE));
    dispatch((0, _chatSettingsSlice.setSelectedUser)(user));
  };
  const handleReplyMessage = () => {
    dispatch((0, _roomsSlice.setEditAction)({
      isEdit: false
    }));
    if (!isReply && message.mainMessage) {
      const messageCore = JSON.parse(message.mainMessage);
      dispatch((0, _roomsSlice.setActiveMessage)({
        id: messageCore.id,
        chatJID: messageCore.roomJid
      }));
      return setIsPressed(false);
    }
    dispatch((0, _roomsSlice.setActiveMessage)({
      id: message.id,
      chatJID: message.roomJid
    }));
    return setIsPressed(false);
  };
  const handleDeleteMessage = () => {
    dispatch((0, _chatSettingsSlice.setDeleteModal)({
      isDeleteModal: true,
      roomJid: message.roomJid,
      messageId: message.id
    }));
    setIsPressed(false);
  };
  const handleEditMessage = () => {
    dispatch((0, _roomsSlice.setEditAction)({
      isEdit: true,
      roomJid: message.roomJid,
      messageId: message.id,
      text: message.body
    }));
    return setIsPressed(false);
  };

  // const handleLongPress = () => {
  //   setIsPressed(true);
  //   console.log("setIsPressed", isPressed);
  // };

  const handleReactionMessage = emoji => {
    if (!message.reaction) {
      return client?.sendMessageReactionStanza(message.id, message.roomJid, [emoji], `${user.firstName} ${user.lastName}`);
    }
    if (message.reaction && message.reaction[user.xmppUsername || ''] && message.reaction[user.xmppUsername || '']?.emoji.includes(emoji)) {
      const filterEmoji = message.reaction[user.xmppUsername || '']?.emoji.filter(reaction => reaction !== emoji);
      return client?.sendMessageReactionStanza(message.id, message.roomJid, filterEmoji, `${user.firstName} ${user.lastName}`);
    }
    client?.sendMessageReactionStanza(message.id, message.roomJid, [...(message.reaction[user.xmppUsername || '']?.emoji || []), emoji], `${user.firstName} ${user.lastName}`);
  };
  const handleLongPress = () => {
    if (messageRef.current) {
      const nodeHandle = (0, _reactNative.findNodeHandle)(messageRef.current);
      if (nodeHandle) {
        _reactNative.UIManager.measure(nodeHandle, (x, y, width, height, pageX, pageY) => {
          const screenHeight = _reactNative.Dimensions.get('window').height;
          const enoughSpaceBelow = screenHeight - pageY - height > 150;
          setContextMenuPosition({
            x: pageX,
            y: enoughSpaceBelow ? pageY + height : pageY - 150
          });
        });
      }
    }
    setIsPressed(true);
  };
  const messageText = config?.messageTextFilter?.enabled ? (0, _parseMessageBody.parseMessageBody)(config?.messageTextFilter.filterFunction(message.body)) : (0, _parseMessageBody.parseMessageBody)(message.body);
  const isPending = idSet.has(message.id) || message?.pending || false;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
    children: [isPressed && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: styles.overlay
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      ref: messageRef,
      style: [styles.customMessageContainer, {
        justifyContent: isUser ? 'flex-end' : 'flex-start',
        marginBottom: !!message?.reply?.length || message?.reaction && !!Object.keys(message?.reaction)?.length ? 20 : 0
      }, isPressed ? {
        transform: [{
          scale: 1.05
        }],
        paddingRight: 16
      } : undefined
      // justify-content: ${({ isUser }) => (isUser ? "flex-end" : "flex-start")},
      // margin-bottom: ${(props) => !!props.reply && "20px"},
      ],
      children: [!isUser && /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomMessagePhotoContainer, {
        onPress: () => handleUserAvatarClick(message.user),
        children: message.user?.profileImage ? /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomMessagePhoto, {
          source: {
            uri: message.user.profileImage
          }
        }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_Avatar.Avatar, {
          username: message.user.name
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Pressable
      // disableInteractions hides the long-press → context menu
      // (delete / edit / reply / react). Mirrors web's config gate.
      , {
        onLongPress: config?.disableInteractions ? undefined : handleLongPress,
        delayLongPress: 500,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(CustomMessageBubble, {
          isUser: isUser,
          deleted: message.isDeleted,
          backgroundMessageUser: config?.messageColor?.backgroundMessageUser,
          backgroundMessage: config?.messageColor?.backgroundMessage,
          children: [!isUser && /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomUserName, {
            color: config?.colors?.primary,
            children: message.user.name
          }), !isReply && message.mainMessage && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageReply.MessageReply, {
            handleReplyMessage: handleReplyMessage,
            isUser: isUser,
            text: JSON.parse(message.mainMessage).text,
            color: config?.colors?.primary
          }), message?.isMediafile === 'true' && !message?.isDeleted ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_MediaMessage.default, {
            mimeType: message.mimetype,
            messageText: message.locationPreview,
            location: message?.location,
            message: message
          }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
            children: message.isDeleted && message.id !== 'delimiter-new' ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_DeletedMessage.DeletedMessage, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)(CustomMessageText, {
              isUser: isUser,
              colorUser: config?.messageColor?.colorUser,
              color: config?.messageColor?.color,
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
                children: messageText
              })
            })
          }), config?.enableTranslates && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageTranslations.default, {
            message: message,
            config: config,
            langSource: langSource
          }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(CustomMessageTimestamp, {
            children: [!config?.disableSentLogic && isUser && isPending && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
              children: "sending..."
            }), new Date(message.date).toLocaleTimeString([], {
              hour: '2-digit',
              minute: '2-digit'
            }), !config?.disableSentLogic && isUser && !isPending && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.DoubleTick, {})]
          }), message?.reply?.length && message?.reply?.length > 0 ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_BottomReplyContainer.BottomReplyContainer, {
            isUser: isUser,
            onClick: handleReplyMessage,
            reply: message?.reply
          }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.MessageFooter, {
            isUser: isUser,
            children: message.reaction && !config?.disableReactions && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageReaction.MessageReaction, {
              reaction: message.reaction,
              changeReaction: handleReactionMessage,
              color: config?.colors?.primary || '#0052CD',
              userName: `${user.firstName} ${user.lastName}`
            })
          })]
        })
      })]
    }), !config?.disableInteractions && isPressed && /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageInteractions.default, {
      position: contextMenuPosition,
      isReply: isReply,
      isUser: isUser,
      message: message,
      closeMenu: () => setIsPressed(false),
      handleReplyMessage: handleReplyMessage,
      handleDeleteMessage: handleDeleteMessage,
      handleEditMessage: handleEditMessage,
      handleReactionMessage: handleReactionMessage
    })]
  });
};
exports.Message = Message;
const styles = _reactNative.StyleSheet.create({
  customMessageContainer: {
    flexDirection: 'row',
    padding: 10,
    alignItems: 'flex-end',
    position: 'relative'
  },
  overlay: {
    ..._reactNative.StyleSheet.absoluteFillObject,
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%'
    // backgroundColor: "rgba(0, 0, 0, 0.3)",
  },
  timestamp: {
    fontSize: 12,
    color: '#999',
    marginTop: 5,
    alignSelf: 'flex-end'
  }
});
//# sourceMappingURL=Message.js.map