import { useCallback } from 'react'

import useSendWithButton from '../useSendWithButton'
import { composeDisposeBody, composeDisposeURL, processDispose } from '../../api/dispose'
import { DisposeParams, isDisposeResponse } from '../../api/dispose/types'

function useDispose(resolve: () => void) {
    const { doSend, button } = useSendWithButton(
        'Выбор сделан',
        'Зачтено',
        true,
        composeDisposeURL(),
        'POST',
        true,
        isDisposeResponse,
        processDispose,
    )

    const doSendWithClose = useCallback(async (...args: DisposeParams) => {
        const res = await doSend({}, {
            body: composeDisposeBody(...args),
            headers: {
                'Content-Type': 'application/json',
            },
        })

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

    return { handleDispose: doSendWithClose, disposeButton: button }
}

export default useDispose
