using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace SharedKernel.Extensions
{
    public static class ModelStateDictionaryExtensions
    {
        public static string GetErrorMessages(this ModelStateDictionary modelState, string separator = "\n")
        {
            var errors = modelState
                .Where(kvp => kvp.Value?.Errors.Count > 0)
                .SelectMany(kvp => kvp.Value!.Errors)
                .Select(e => string.IsNullOrWhiteSpace(e.ErrorMessage) ? "Invalid value." : e.ErrorMessage)
                .ToArray();

            return string.Join(separator, errors);
        }
    }
}