using System;
using System.Text.RegularExpressions;

namespace eagle {
    public readonly struct DynamicTextPlaceholderName : IEquatable<DynamicTextPlaceholderName> {
        private static readonly Regex ValidName =
                new(@"^\p{L}[\p{L}\p{Nd}\p{Mn}\p{Mc}\p{Pc}\p{Cf}]*$",
                    RegexOptions.CultureInvariant);

        public string Value { get; }

        public DynamicTextPlaceholderName(string value) {
            if (!IsValidName(value)) {
                throw new ArgumentException(
                        $"Invalid dynamic text placeholder name: {value}",
                        nameof(value));
            }

            Value = value;
        }

        public static bool IsValidName(string value) {
            return !string.IsNullOrEmpty(value) && ValidName.IsMatch(value);
        }

        public bool Equals(DynamicTextPlaceholderName other) { return Value == other.Value; }

        public override bool Equals(object obj) {
            return obj is DynamicTextPlaceholderName other && Equals(other);
        }

        public override int GetHashCode() { return Value.GetHashCode(); }

        public DynamicTextPlaceholderName WithNumericSuffix(int suffix) {
            return new DynamicTextPlaceholderName($"{Value}{suffix}");
        }

        public override string ToString() { return Value; }
    }
}
