import { useCallback } from 'react'

import useSend from './useSend'
import useSendButtonCaption from './useSendButtonCaption'

function useSendWithButton<R, T extends NonNullable<unknown>>(
    initial: string,
    result: string,
    singular?: boolean,
    ...useSendArgs: Parameters<typeof useSend<R, T>>
) {
    const { doSend, loading, error } = useSend(...useSendArgs)

    const { update, ...button } = useSendButtonCaption(initial, loading, error, result, singular)

    const doSendWithButton = useCallback(async (...args: Parameters<typeof doSend>) => {
        const data = await doSend(...args)

        update(data)

        return data
    }, [doSend, update])

    return { doSend: doSendWithButton, button }
}

export default useSendWithButton
