import React, { createContext, useContext } from 'react'

/** @type {import('react').Context<import('./Form.jsx').FormContextValue | null>} */
export const FormContext = createContext(null)

/**
 * @returns {import('./Form.jsx').FormContextValue | null}
 */
export function useOptionalFormContext () {
  return useContext(FormContext)
}

/**
 * @returns {import('./Form.jsx').FormContextValue}
 */
export function useFormContext () {
  const ctx = useContext(FormContext)
  if (!ctx) {
    throw new Error('useFormContext must be used within a Form')
  }
  return ctx
}
