import { ref } from 'vue'

export default (url) => {
    const loading = ref(false)
    const success = ref(false)
    const error = ref('')

    const submit = async (headers = {}, body = {}) => {
        loading.value = true

        try {
            await fetch(url, {
                method: 'POST',
                headers,
                body: new URLSearchParams(body),
            })

            success.value = true
            error.value = ''
        } catch (err) {
            console.error(err)
            error.value = err.toString().split('Error: ')[1]
        }

        loading.value = false
    }

    return {
        loading,
        success,
        error,
        submit,
    }
}
