import { createContext, ReactNode, useState } from 'react'
import PopupConfirm from './PopupConfirm'

interface ConfirmContextProps {
  confirm: {
    prompt: ReactNode
    isOpen: boolean
    proceed: () => void
    cancel: () => void
  }
  setConfirm: any
}

export const ConfirmContext = createContext<ConfirmContextProps>({
  confirm: {
    cancel: () => {},
    proceed: () => {},
    prompt: 'Are you sure?',
    isOpen: false,
  },
  setConfirm: () => {},
})

const ConfirmContextProvider = ({ children }) => {
  const [confirm, setConfirm] = useState({
    prompt: '',
    isOpen: false,
    proceed: null,
    cancel: null,
  })

  return (
    <ConfirmContext.Provider value={{ confirm, setConfirm }}>
      {children}
      <PopupConfirm />
    </ConfirmContext.Provider>
  )
}

export default ConfirmContextProvider
