import { createContext, useContext } from "solid-js";

/**
 * Mapping of field name → error(s). Populated by `<Form>` after server-side
 * validation or external submission feedback.
 */

/**
 * Registration record that field controls push into `<Form>` so that `<Form>`
 * can orchestrate cross-field validation / submission.
 */

/**
 * Opaque handle held by `<Form>` so field controls have a stable place to
 * register without going through the reactive graph. The `fields` map is
 * mutated directly by field controls — do NOT put it in a signal or store.
 */

const NOOP = () => {};

/**
 * Factory for a fresh noop form context. Each call builds a new `fields`
 * map so tests (and real code paths) that imperatively mutate
 * `formRef.current.fields` can't leak state across runs via a shared
 * module-load singleton.
 *
 * `useFormContext` calls this when no `<Form>` provider is above, so every
 * consumer receives its own noop instance.
 */
export function createNoopFormContext() {
  return {
    formRef: {
      current: {
        fields: new Map()
      }
    },
    errors: () => ({}),
    setErrors: NOOP,
    clearErrors: NOOP,
    validationMode: () => "onSubmit",
    submitAttemptedRef: {
      current: false
    },
    present: false
  };
}

/**
 * Identity-only sentinel. `useFormContext` compares against this to detect
 * "no `<Form>` provider above" and return a fresh noop instead of the
 * shared default — preventing cross-call state leaks via `fields`
 * mutations. The sentinel itself is never returned from the hook.
 *
 * Exported as `NOOP_FORM_CONTEXT` for barrel / compat only; application
 * code should call `useFormContext()` and branch on `.present`.
 */
export const NOOP_FORM_CONTEXT = createNoopFormContext();

/**
 * Context carrying `<Form>` state. Provided via `<FormContext value={...}>`.
 * Default value is the shared sentinel — `useFormContext` detects it by
 * identity and substitutes a fresh noop.
 */
export const FormContext = createContext(NOOP_FORM_CONTEXT);

/**
 * Read the current `<Form>` context. When unwrapped (no `<Form>` ancestor),
 * returns a fresh noop so callers that mutate `formRef.current.fields` can't
 * inadvertently share state with other roots. Callers can branch on
 * `.present` to detect "no form".
 */
export function useFormContext() {
  const ctx = useContext(FormContext);
  if (ctx === NOOP_FORM_CONTEXT) {
    return createNoopFormContext();
  }
  return ctx;
}
//# sourceMappingURL=form-context.jsx.map
