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

namespace SGModule.NetKit {
    public static class TrackEvent {
        private static readonly HashSet<string> _registeredStaticEvents = new();
        private static readonly HashSet<string> _registeredDynamicEventPrefixes = new();


        public static string Register(string eventKey, bool isDynamic = false) {
            var targetSet = isDynamic ? _registeredDynamicEventPrefixes : _registeredStaticEvents;

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

            return eventKey;

        }


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

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

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