import type { PaymentIntentResult } from '@stripe/stripe-js'
import { useStripe } from '@stripe/react-stripe-js'

export const useStripe3DSecure = () => {
  const stripe = useStripe()

  const handleConfirmationResult = (result: PaymentIntentResult) => {
    if (result.error) {
      return Promise.reject(result.error)
    }

    return Promise.resolve(result.paymentIntent)
  }

  const handle3DSecure = (clientSecret: string) => (
    stripe?.confirmCardPayment(clientSecret).then(handleConfirmationResult)
  )

  return { handle3DSecure }
}
