import { useZocksDispatch } from '../../app/hooks'
import { useZocksStore } from '../../app'
import { selectPrivateChatMessages, selectPublicChatMessages, ZocksChatActions } from '../index'
import { clearNewMessageFrom, selectMessageRecipient, selectNewMessagesFrom } from './chatSlice'

export const useChat = (chatActions: ZocksChatActions) => {
  const { sendPublicMessage, sendPrivateMessage, uploadAttachment, getAttachmentLocation } = chatActions

  const publicChatMessages = useZocksStore(selectPublicChatMessages)
  const privateChatMessages = useZocksStore(selectPrivateChatMessages)
  const messageRecipient = useZocksStore(selectMessageRecipient)

  return {
    sendPublicMessage,
    sendPrivateMessage,
    uploadAttachment,
    getAttachmentLocation,
    publicChatMessages,
    privateChatMessages,
    messageRecipient,
  }
}

export const useNewMessagesFrom = () => {
  const dispatch = useZocksDispatch()
  const newMessagesFrom = useZocksStore(selectNewMessagesFrom)

  const clearMessageFrom = (messageFrom: string) => {
    dispatch(clearNewMessageFrom(messageFrom))
  }

  return {
    newMessagesFrom,
    clearMessageFrom,
  }
}
