// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Text.RegularExpressions;

namespace Microsoft.AspNetCore.Razor.Language
{
	public static class HtmlConventions
	{
		private const string HtmlCaseRegexReplacement = "-$1$2";
		private static readonly char[] InvalidNonWhitespaceHtmlCharacters =
			new[] { '@', '!', '<', '/', '?', '[', '>', ']', '=', '"', '\'', '*' };

		// This matches the following AFTER the start of the input string (MATCH).
		// Any letter/number followed by an uppercase letter then lowercase letter: 1(Aa), a(Aa), A(Aa)
		// Any lowercase letter followed by an uppercase letter: a(A)
		// Each match is then prefixed by a "-" via the ToHtmlCase method.
		private static readonly Regex HtmlCaseRegex =
			new Regex(
				"(?<!^)((?<=[a-zA-Z0-9])[A-Z][a-z])|((?<=[a-z])[A-Z])",
				RegexOptions.None,
				TimeSpan.FromMilliseconds( 500 ) );


		internal static bool IsInvalidNonWhitespaceHtmlCharacters( char testChar )
		{
			foreach ( var c in InvalidNonWhitespaceHtmlCharacters )
			{
				if ( c == testChar )
				{
					return true;
				}
			}

			return false;
		}

		/// <summary>
		/// Converts from pascal/camel case to lower kebab-case.
		/// </summary>
		/// <example>
		/// SomeThing => some-thing
		/// capsONInside => caps-on-inside
		/// CAPSOnOUTSIDE => caps-on-outside
		/// ALLCAPS => allcaps
		/// One1Two2Three3 => one1-two2-three3
		/// ONE1TWO2THREE3 => one1two2three3
		/// First_Second_ThirdHi => first_second_third-hi
		/// </example>
		public static string ToHtmlCase( string name )
		{
			return HtmlCaseRegex.Replace( name, HtmlCaseRegexReplacement ).ToLowerInvariant();
		}
	}
}
