import { ReactNode, useContext, useEffect, useState } from 'react'
import { ConfirmContext } from './ConfirmContextProvider'

export default function useConfirm(): {
  isConfirmed: (prompt?: string) => Promise<boolean>
  prompt?: ReactNode
  isOpen?: boolean
  proceed?: () => void
  cancel?: () => void
} {
  const { confirm, setConfirm } = useContext(ConfirmContext)
  const [needsCleanup, setNeedsCleanup] = useState(false)

  const isConfirmed = (prompt) => {
    setNeedsCleanup(true)
    const promise = new Promise((resolve, reject) => {
      setConfirm({
        prompt,
        isOpen: true,
        proceed: resolve,
        cancel: reject,
      })
    })
    return promise.then(
      () => {
        setConfirm({ ...confirm, isOpen: false })
        return true
      },
      () => {
        setConfirm({ ...confirm, isOpen: false })
        return false
      },
    )
  }

  useEffect(() => {
    return () => {
      if (confirm.cancel && needsCleanup) {
        confirm.cancel()
      }
    }
  }, [confirm, needsCleanup])
  return {
    ...confirm,
    isConfirmed,
  }
}
