import type { ZodError } from "zod";

/**
 * Flattens a zod validation error into `{ fieldName: firstMessage }` for
 * inline display under each form field — verbatim copy of apps/web's
 * `lib/zod-errors.ts` (only the first message per field, enough for the
 * single-rule-per-field schemas used here).
 */
export function fieldErrorsFrom(error: ZodError): Record<string, string> {
  const fieldErrors = error.flatten().fieldErrors;
  const firstMessagePerField: Record<string, string> = {};
  for (const [field, messages] of Object.entries(fieldErrors)) {
    if (messages?.[0]) {
      firstMessagePerField[field] = messages[0];
    }
  }
  return firstMessagePerField;
}
