/**
 * useMessageInput — gerencia o estado e envio da barra de mensagem.
 *
 * Extrai de inbox/index.tsx:
 *  - newMessage + setNewMessage
 *  - handleSendMessage
 */
import { useState, useCallback } from 'react'
import type { NewWhatsChat } from '../../types/inboxTypes'

interface UseMessageInputOptions {
  selectedChat: NewWhatsChat | null
  replyingTo?: { message_id: string } | null
  onClearReply?: () => void
  onSendText: (text: string, mentions?: string[], replyToMessageId?: string) => Promise<void>
}

export function useMessageInput({ selectedChat, replyingTo, onClearReply, onSendText }: UseMessageInputOptions) {
  const [newMessage, setNewMessage] = useState('')
  const [mentions, setMentions] = useState<string[]>([])

  const handleSendMessage = useCallback(async (e?: React.FormEvent) => {
    e?.preventDefault()
    if (!selectedChat || !newMessage.trim()) return
    const text = newMessage
    const mentionJids = mentions.length > 0 ? [...mentions] : undefined
    const replyToMessageId = replyingTo?.message_id
    setNewMessage('')
    setMentions([])
    onClearReply?.()
    await onSendText(text, mentionJids, replyToMessageId)
  }, [selectedChat, newMessage, mentions, replyingTo, onSendText, onClearReply])

  return {
    newMessage,
    setNewMessage,
    mentions,
    setMentions,
    handleSendMessage,
  }
}
