using System.Collections.Generic;
using System.Linq;
using SGModule.Common.Helper;

namespace SGModule.NetKit {
    public static class TrackProperty {
        private static readonly HashSet<string> _registeredStaticProperties = new();
        private static readonly HashSet<string> _registeredDynamicPropertyPrefixes = new();

        public static string Register(string propertyKey, bool isDynamic = false) {
            var targetSet = isDynamic ? _registeredDynamicPropertyPrefixes : _registeredStaticProperties;

            if (!targetSet.Add(propertyKey)) {
                var type = isDynamic ? "动态" : "静态";
                Log.NetKit.Error($"[Track] 重复的{type} Property 注册: {propertyKey}");
            }

            return propertyKey;
        }


        /// <summary>
        /// 是否为注册的静态 property（完全匹配）
        /// </summary>
        private static bool ContainsExact(string propertyKey) {
            return _registeredStaticProperties.Contains(propertyKey);
        }

        /// <summary>
        /// 是否匹配任何已注册的动态前缀
        /// </summary>
        private static bool ContainsDynamic(string propertyKey) {
            return _registeredDynamicPropertyPrefixes.Any(propertyKey.StartsWith);
        }

        /// <summary>
        /// 检查是否存在（静态或动态）
        /// </summary>
        public static bool Contains(string propertyKey) {
            return ContainsExact(propertyKey) || ContainsDynamic(propertyKey);
        }
    }
}
