import type { ZodError } from "zod";

/**
 * Flattens a zod validation error into `{ fieldName: firstMessage }`, for
 * simple inline display under each form field (only the first message per
 * field is shown — good enough for the single-rule-per-field schemas this
 * app uses today).
 */
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;
}
