// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using static OSS.Blazor.Internal.LinkerFlags;
namespace OSS.Blazor.Components.Routing;
///
/// This class has been enhanced to add more functionality when using 'CustomRoute'.
/// The RouteContext class is now 'Public'.
/// The microsoft version is 'internal'.
///
public sealed class RouteContext // <-- was: 'internal' sealed class
{
///
/// This class has been enhanced to add more functionality when using the AAouter Component 'CustomRoute' parameter.
/// Is backwards compatible with existing microsoft version.
///
/// Can contains the full absoulte Uri.
/// Is Navigation Intercepted.
/// Route context options. default = 1 ie RemoveEmptyEntries.
public RouteContext(string path, bool? isNavigationIntercepted = null, int? options = null)
{
//-------------------------------------------------------------------
Path = path;
IsNavigationIntercepted = isNavigationIntercepted;
//-------------------------------------------------------------------
var firstIndex = path.AsSpan().IndexOfAny('?', '#');
if(firstIndex > -1) path = path[..firstIndex];
//-------------------------------------------------------------------
// This is a simplification.
// We are assuming there are no paths like /a//b/. <-- set options = 1 to include empty routes
// A proper routing implementation would be more sophisticated.
Segments = path.ToString().Trim('/').Split('/', (StringSplitOptions)(options ?? 1));
// Individual segments are URL-decoded in order to support arbitrary characters, assuming UTF-8 encoding.
for (int i = 0; i < Segments.Length; i++)
{
Segments[i] = Uri.UnescapeDataString(Segments[i]);
}
}
//-----------------------------------------------------------------------
///
/// Can contain the BaseRelativePath (ie PathAndQuery)
///
public string? Path { get; }
///
/// Can be used to store the 'IsNavigationIntercepted' state.
/// means unknown or out of scope.
///
public bool? IsNavigationIntercepted { get; set; }
///
/// If applicable, this index can be used to indicate which segment represents the Handler Component; else leave .
/// means unknown or out of scope.
///
public int? HandlerSegmentIndex { get; set; }
public bool EndRouting { get; set; }
//-----------------------------------------------------------------------
public string[] Segments { get; }
[DynamicallyAccessedMembers(Component)]
public Type? Handler { get; set; }
public IReadOnlyDictionary? Parameters { get; set; }
}