import { ApiError, ApiErrorItem } from '../services/api.service';

export function getErrorMessage(err: ApiError, fallback: string): string {
  const errors = err?.errors;
  if (!errors) return fallback;

  // Array format: [{ propertyName, errorMessage }]
  if (Array.isArray(errors)) {
    return errors[0]?.errorMessage ?? fallback;
  }

  // Dictionary format: { field: string[] }
  const first = Object.values(errors).flat()[0];
  return (typeof first === 'string' ? first : null) ?? fallback;
}
