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

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

//**************************************************************************************************
/// <summary>
/// Culture restoring helpers
/// </summary>
static class WithCulture {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentNullException" />
static void Do(CultureInfo? culture,Action body) =>
   Do( culture, null, body );
/// <exception cref="ArgumentNullException" />
static R Do<R>(CultureInfo? culture,Func<R> body) =>
   Do<R>( culture, null, body );

/// <exception cref="ArgumentNullException" />
static void Do(CultureInfo? culture,bool affectUICulture,Action body) =>
   Do( culture, affectUICulture ? culture : null, body );
/// <exception cref="ArgumentNullException" />
static R Do<R>(CultureInfo? culture,bool affectUICulture,Func<R> body) =>
   Do<R>( culture, affectUICulture ? culture : null, body );

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

/// <exception cref="ArgumentNullException" />
static void Do(CultureInfo? culture,CultureInfo? uiCulture,Action body)
{
   HArgChecking.VerifyNotNull( body );
   var thread = HThread.CurrentThread;
   CultureInfo?
      savedCulture = culture!=null ? thread.CurrentCulture : null,
      savedUICulture = uiCulture!=null ? thread.CurrentUICulture : null;
   try {
      if ( culture != null )
         thread.CurrentCulture = culture;
      if ( uiCulture != null )
         thread.CurrentUICulture = uiCulture;
      body();
   }
   finally {
      if ( savedUICulture != null )
         thread.CurrentUICulture = savedUICulture;
      if ( savedCulture != null )
         thread.CurrentCulture = savedCulture;
   }
}

/// <exception cref="ArgumentNullException" />
static R Do<R>(CultureInfo? culture,CultureInfo? uiCulture,Func<R> body)
{
   HArgChecking.VerifyNotNull( body );
   var thread = HThread.CurrentThread;
   CultureInfo?
      savedCulture = culture!=null ? thread.CurrentCulture : null,
      savedUICulture = uiCulture!=null ? thread.CurrentUICulture : null;
   try {
      if ( culture != null )
         thread.CurrentCulture = culture;
      if ( uiCulture != null )
         thread.CurrentUICulture = uiCulture;
      return body();
   }
   finally {
      if ( savedUICulture != null )
         thread.CurrentUICulture = savedUICulture;
      if ( savedCulture != null )
         thread.CurrentCulture = savedCulture;
   }
}

//==================================================================================================

/// <exception cref="ArgumentNullException" />
static void Do(string? cultureStr,Action body) =>
   Do( cultureStr, null, body );
/// <exception cref="ArgumentNullException" />
static R Do<R>(string? cultureStr,Func<R> body) =>
   Do<R>( cultureStr, null, body );

/// <exception cref="ArgumentNullException" />
static void Do(string? cultureStr,bool affectUICulture,Action body) =>
   Do( cultureStr, affectUICulture ? cultureStr : null, body );
/// <exception cref="ArgumentNullException" />
static R Do<R>(string? cultureStr,bool affectUICulture,Func<R> body) =>
   Do<R>( cultureStr, affectUICulture ? cultureStr : null, body );

/// <exception cref="ArgumentNullException" />
static void Do(string? cultureStr,string? uiCultureStr,Action body) =>
   Do( cultureStr!=null ? new CultureInfo(cultureStr) : null,
       uiCultureStr!=null ? new CultureInfo(uiCultureStr) : null,
       body );
/// <exception cref="ArgumentNullException" />
static R Do<R>(string? cultureStr,string? uiCultureStr,Func<R> body) =>
   Do<R>( cultureStr!=null ? new CultureInfo(cultureStr) : null,
          uiCultureStr!=null ? new CultureInfo(uiCultureStr) : null,
          body );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // WithCulture
//**************************************************************************************************

} // CommonHelpers (.WASM)
