import { useCallback } from 'react'

import { useSendWithButton } from '..'
import { composeBookURL, processBook } from '../../api/book'
import { isBookResponse } from '../../api/book/types'

function useBook() {
    const { doSend, button } = useSendWithButton('Забронировать',
        'Забронировано',
        true,
        composeBookURL(),
        'POST',
        true,
        isBookResponse,
        processBook,
    )

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

    return { handleBook, bookButton: button }
}

export default useBook
