using MudBlazor.Interfaces;
namespace AIStudio.Tools.Validation;
///
/// Gives form fields a fresh verdict when a field they are checked against changes.
///
///
/// A MudForm checks a field only when that very field changes. Where the rule of one field reads
/// another one -- the required confidence level is checked against the embedding provider, and so
/// are the token limits -- changing the other field leaves the verdict as it was, and an error which
/// no longer holds stays on screen. Hooked to the FieldChanged event of the form, this checks the
/// dependent fields again after every change. A state which is no field of the form, such as
/// showing the expert settings, passes no changed field at all.
///
public static class DependentFieldValidation
{
///
/// Validates the dependent fields anew.
///
///
/// The field which just changed is skipped, because the form checks it anyway. So is every
/// field the user has not reached yet and which shows no error: a dialog just opened must not
/// greet anybody with errors about fields they have not filled in.
///
/// A field checks the value it holds itself, and a value set in code reaches it only with the
/// next render. Where the code changes a value before asking for a fresh verdict, it asks after
/// that render, i.e., in OnAfterRenderAsync; asked at once, the field would judge the old value.
///
/// The field the form reported as changed, or null when the change happened outside the fields.
/// The fields whose rules read other fields. A field which is not rendered yet is null.
public static async Task RevalidateAsync(IFormComponent? changedField, params IFormComponent?[] dependentFields)
{
foreach (var field in dependentFields)
{
if (field is null || ReferenceEquals(field, changedField))
continue;
if (field.Touched || field.HasErrors)
await field.Validate();
}
}
}