using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.WebUtilities;

namespace DiplomovaPrace.Web.Admin.Infrastructure
{
	public static class NavigationManagerExtension
	{
		public static T ExtractQueryStringByKey<T>(this NavigationManager navigationManager, string key)
		{
			var uri = navigationManager.ToAbsoluteUri(navigationManager.Uri);
			QueryHelpers.ParseQuery(uri.Query).TryGetValue(key, out var queryValue);

			if (typeof(T).Equals(typeof(int)))
			{
				int.TryParse(queryValue, out int result);
				return (T)(object)result;
			}

			if (typeof(T).Equals(typeof(string)))
			{
				return (T)(object)queryValue.ToString();
			}

			return default;
		}
	}
}
