using System.Reflection;
namespace BotPages.Core.Routing;
///
/// Реестр маршрутов страниц.
///
internal sealed class RoutesRegistry
{
private readonly Dictionary _routes = new(StringComparer.OrdinalIgnoreCase);
///
/// Зарегистрировать маршрут для страницы.
///
public void Map(string template) where TPage : Page
{
if (_routes.ContainsKey(template))
throw new InvalidOperationException($"Route '{template}' is already mapped.");
_routes[template] = typeof(TPage);
}
///
/// Найти страницу по маршруту.
///
public Type? Resolve(string template) => _routes.TryGetValue(template, out var t) ? t : null;
///
/// Получить снимок всех маршрутов.
///
public IReadOnlyDictionary Snapshot() => _routes;
internal void Map(Type? type)
{
foreach(var attr in type.GetCustomAttributes(inherit: true))
{
_routes.Add(attr.Template, type);
}
_routes.Add(type.FullName, type);
}
}