import { useCallback } from 'react'

import { useSend } from '..'
import { composePutAnnouncementURL, processPutAnnouncement } from '../../api/putAnnouncement'
import { isPutAnnouncementResponse } from '../../api/putAnnouncement/types'
import useSendButtonCaption from '../useSendButtonCaption'

const useAddAnnouncement = () => {
    const { doSend, loading, error } = useSend(
        composePutAnnouncementURL(),
        'PUT',
        true,
        isPutAnnouncementResponse,
        processPutAnnouncement,
    )

    const { update, ...button } = useSendButtonCaption('Опубликовать', loading, error, 'Опубликовано')

    const doSendWithButton = useCallback(async (formData: FormData) => {
        const data = await doSend({}, {
            body: formData
        })
        update(data)

        return data
    }, [doSend, update])

    return { doSend: doSendWithButton, button }
}

export default useAddAnnouncement
