import { useCallback } from 'react'

import useSendWithButton from '../useSendWithButton'
import { composeRemoveAnnouncementURL, processRemoveAnnouncement } from '../../api/removeAnnouncement'
import { isRemoveAnnouncementResponse } from '../../api/removeAnnouncement/types'

function useRemoveAnnouncement(resolve: () => void) {
    const { doSend, button } = useSendWithButton(
        'Закрыть объявление',
        'Закрыто',
        true,
        composeRemoveAnnouncementURL(),
        'DELETE',
        true,
        isRemoveAnnouncementResponse,
        processRemoveAnnouncement,
    )

    const doSendWithClose = useCallback(async (id: number) => {
        const res = await doSend({}, {
            body: JSON.stringify({
                id,
            }),
            headers: {
                'Content-Type': 'application/json',
            },
        })

        if (res) {
            resolve()
        }
    }, [doSend, resolve])

    return { handleRemove: doSendWithClose, removeButton: button }
}

export default useRemoveAnnouncement
