import { MessageEditedSocket } from 'src/types/chats';
import { updateMessage } from 'src/utils/chats';

import { forEachChatDetailMessage } from 'src/components/dashboard/chat/queries';

export const editMessage = (data: MessageEditedSocket) => {
  const { id, chatId } = data;

  forEachChatDetailMessage(Number(chatId), oldMessage => {
    if (!oldMessage) return oldMessage;

    if (oldMessage.id === id) {
      updateMessage(oldMessage, data);
    } else if (oldMessage.replyMessageId === id) {
      updateMessage(oldMessage.replyMessage, data);
    }

    return oldMessage;
  });
};
