// Generated by @typespec/http-server-csharp
// <auto-generated />
#nullable enable

using System.Text.Json;
using System.Text.Json.Serialization;

namespace TypeSpec.Helpers.JsonConverters
{
    /// <summary>
    /// Constrains an array of strings
    /// </summary>
    public class StringArrayConstraintAttribute : ArrayConstraintAttribute<string>
    {
        int? _minItemLength = null, _maxItemLength = null;

        public StringArrayConstraintAttribute() : base() { }

        public int MinItemLength
        {
            get { return _minItemLength.HasValue ? _minItemLength.Value : 0; }
            set { _minItemLength = value; }
        }
        public int MaxItemLength
        {
            get { return _maxItemLength.HasValue ? _maxItemLength.Value : 0; }
            set { _maxItemLength = value; }
        }
        public string? Pattern { get; set; }

        public override JsonConverter? CreateConverter(Type typeToConvert)
        {
            var result = base.CreateConverter(typeToConvert);
            var resultSet = result as ConstrainedSetConverter<string>;
            if (resultSet != null)
            {
                resultSet.InnerConverterFactory = (c, o) =>
                    new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
                return resultSet;
            }

            var resultEnumerable = result as ConstrainedEnumerableConverter<string>;
            if (resultEnumerable != null)
            {
                resultEnumerable.InnerConverterFactory = (c, o) =>
                    new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
                return resultEnumerable;
            }

            var resultStandardArray = result as ConstrainedStandardArrayConverter<string>;
            if (resultStandardArray != null)
            {
                resultStandardArray.InnerConverterFactory = (c, o) =>
                    new StringJsonConverter(MinItemLength, MaxItemLength, Pattern, o);
                return resultStandardArray;
            }
            throw new InvalidOperationException($"Cannot create converter for {typeToConvert} with {this}");
        }
    }
}
