using System.Text.RegularExpressions;
using NodePipeline.Abstractions.Interfaces;
using NodePipeline.Abstractions.Interfaces.Nodes;
using NodePipeline.Abstractions.Models.Validation;
using NodePipeline.Abstractions.Validators;

namespace NodePipeline.Application;

public partial class ExtensionValidator(IPipelineLocalizationProvider localizer)
    : NodeFieldValidatorBase<string>(localizer)
{
    public override NodeFieldValidationResult Validate(INodeField<string> nodeField)
    {
        return ExtensionRegex().IsMatch(nodeField.Value)
            ? SuccessResult
            : new NodeFieldValidationResult(ValidationResult.HasErrors,
                Localizer.GetLocalizedString("Extension", Culture, nodeField.Value, ExtensionRegex().ToString()));
    }

    [GeneratedRegex("^\\.?.+$")]
    private static partial Regex ExtensionRegex();
}