import { useSendWithButton } from '..'
import { composeSignUpURL, processSignUp } from '../../api/signup'
import { SignUpBody, isSignUpResponse } from '../../api/signup/types'

function useSignUp() {
    const { doSend, button } = useSendWithButton(
        'Зарегистрироваться',
        'Зарегистрирован',
        false,
        composeSignUpURL(),
        'POST',
        false,
        isSignUpResponse,
        processSignUp,
    )

    async function handleSignUp(data: SignUpBody) {
        const res = await doSend({}, {
            body: JSON.stringify(data),
            headers: {
                'Content-Type': 'application/json',
            },
        })

        return res ?? false
    }

    return { handleSignUp, signUpButton: button }
}

export default useSignUp
