//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Threading;
using System.Globalization;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
partial class HChar {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool IsNativeLetter(char ch) =>
   IsNativeLetter( ch, CultureInfo.CurrentCulture );
public static bool IsNativeLetter(char ch,[HOptional] CultureInfo culture)
{
   try
      { return StrictIsNativeLetter( ch, culture ); }
   catch (NotSupportedException)
      { return false; }
}

/// <exception cref="NotSupportedException" />
[HStrictResult]
public static bool StrictIsNativeLetter(char ch) =>
   StrictIsNativeLetter( ch, CultureInfo.CurrentCulture );
/// <exception cref="NotSupportedException" />
[HStrictResult]
public static bool StrictIsNativeLetter(char ch,[HOptional] CultureInfo culture) =>
   IsNativeLetterX( ch, culture ) ??
   throw HCultureInfo.CreateNotSupportedOperationException( culture );

public static bool? IsNativeLetterX(char ch,[HOptional] CultureInfo culture)
{
   var ci = culture ?? CultureInfo.InvariantCulture;

   switch ( HCultureInfo.ToNeutral( ci ).Name )
   {
   case HNeutralCultureNames.English:
      return HAscii.IsLetter( ch );
   }

   switch ( ci.Name )
   {
   case HSpecificCultureNames.ru_RU:
      return
         HOps.IsBetween( ch, 'А', 'Я' ) ||
         HOps.IsBetween( ch, 'а', 'я' ) ||
         HOps.IsOneOf( ch, 'Ё', 'ё' );
   }

   return null;
}

//--------------------------------------------------------------------------------------------------

public static bool IsNativeOrEnglishLetter(char ch) =>
   IsNativeOrEnglishLetter( ch, null );
public static bool IsNativeOrEnglishLetter(char ch,[HOptional] CultureInfo culture) =>
   IsNativeLetter( ch, culture ) || IsEnglishLetter( ch );

//==================================================================================================
#if WIN32
public static bool IsInSystemACP(char ch)
{
   int usedDefaultChar;
   var multiByteStrBuff = new byte[ 8 ];
   return Win32_Core.WideCharToMultiByte(
      Win32_Core.CP_ACP, Win32_Core.WC_NO_BEST_FIT_CHARS,
      ch.ToString(), -1,
      multiByteStrBuff, multiByteStrBuff.Length,
      IntPtr.Zero, out usedDefaultChar ) != 0 &&
      usedDefaultChar == 0;
}
#endif // WIN32
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HChar
//**************************************************************************************************

} // CommonHelpers (.WASM)
