using System;
using u = user_rtx;

namespace Test {

/// <summary>
/// Extension methods (internal static class)
/// </summary>
static class Extensions {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// NOT-NULL OBJECT AND VALUE INTERPRETATION (STRICT ACCESSORS):

/// <summary>
/// Not-null object interpretation (strict accessor)
/// </summary>
/// <exception cref="NullReferenceException" />
[u.strict_result]
public static T NN<T>( this T? sourceInstance )
where T: class =>
   u.not_null._<T>( sourceInstance );

/// <summary>
/// Not-null object interpretation (strict accessor)
/// </summary>
/// <exception cref="u.EZeroValue" />
[u.strict_result][u.returns_source]
public static IntPtr NZ( this IntPtr sourcePtr ) =>
   u.not_null._( sourcePtr );

/// <summary>
/// Not-null value interpretation (strict accessor)
/// </summary>
/// <exception cref="u.ENullValue" />
[u.strict_result]
public static T V<T>( this T? sourceNullableValue )
where T: struct =>
   u.not_null.V<T>( sourceNullableValue );

//==================================================================================================
// STRING REPRESENTATION:

/// <summary>
/// String representation (based on “System.Object.ToString”)
/// </summary>
/// <exception cref="u.ENullValue" />
public static string STR( this object? obj, bool throwOnNull=true )
{
   if ( obj == null && !throwOnNull )
      return "";
   var str = obj.NN().ToString();
   return str ?? (throwOnNull ? str.NN() : "");
}

//==================================================================================================
// MULTILINE TEXT NORMALIZATION:

/// <summary>
/// Text normalization (CR,LF)
/// </summary>
public static string TXT( this string multilineStr ) =>
   u.str.TXT( multilineStr );

/// <summary>
/// Text normalization (CR,LF) for nullable string
/// </summary>
public static string? TXT_N( this string? multilineStr ) =>
   u.str.TXT_N( multilineStr );
/// <summary>
/// Text normalization (CR,LF), or empty result (for null argument)
/// </summary>
public static string TXT_E( this string? multilineStr ) =>
   u.str.TXT_E( multilineStr );

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

/// <summary>
/// Text format with normalization (CR,LF)
/// </summary>
public static string FMT( this string messageFmt, params object?[] args ) =>
   u.str.FMT( messageFmt, args );

//==================================================================================================
// EXCEPTION ERROR MESSAGE:

/// <summary>
/// Error message (chained message to display)
/// </summary>
public static string EM( this Exception e ) =>
   u.except.EM( e );
/// <summary>
/// Error message (chained message to display)
/// </summary>
public static string EM( this Exception e, params Type[] packingExceptionTypes ) =>
   u.except.EM( e, packingExceptionTypes );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Extensions

} // Test
