//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.Text;
using System.Collections.Generic;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

/// <summary>
/// Text builder for multiline string composition (static helpers)
/// </summary>
public static class text {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static bool AppendLine( StringBuilder sb )
{
   if ( sb.Length == 0 )
      return false;
   sb.AppendLine();
   return true;
}

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

public static void AppendLine( StringBuilder sb, string str ) =>
   AppendLine( sb, false, str );

public static void AppendLine( StringBuilder sb, bool separate, string str )
{
   if ( sb.Length > 0 ) {
      AppendLine( sb );
      if ( separate )
         AppendLine( sb );
   }
   sb.Append( str.TXT() );
}

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

public static void AppendLine( StringBuilder sb, string format, params object?[] args ) =>
   AppendLine( sb, false, format, args );

public static void AppendLine( StringBuilder sb, bool separate,
                               string format, params object?[] args ) =>
   AppendLine( sb, separate, string.Format( format, args ) );

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

[u.returns_source]
public static StringBuilder AppendAsSingleLine( StringBuilder sb, string multilineStr ) =>
   AppendAsSingleLine( sb, true, multilineStr );
[u.returns_source]
public static StringBuilder AppendAsSingleLine( StringBuilder sb,
                                                [u.def_true] bool insertLineIfNotEmpty,
                                                string multilineStr )
{
   if ( insertLineIfNotEmpty )
      AppendLine( sb );
   foreach ( char ch in multilineStr )
      if ( ch != '\r' )
         sb.Append( ch != '\n' ? ch : ' ' );
   return sb;
}

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

public static string CombineLines( params string[] strings ) =>
   CombineLines( false, strings );

public static string CombineLines( bool separate, params string[] strings )
{
   var sb = new StringBuilder();
   foreach ( var str in strings )
      AppendLine( sb, separate, str );
   return sb.ToString();
}

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

public static StringBuilder TXT( StringBuilder multilineBld )
{
   var sb = new StringBuilder();
   for ( int i=0; i<multilineBld.Length; i++ ) {
      char ch = multilineBld[ i ];
      if ( ch == '\n' )
         sb.AppendLine();
      else
         if ( ch != '\r' )
            sb.Append( ch );
   }
   return sb;
}

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

public static List<string> ToLines( string multilineStr ) =>
   ToLines( new StringBuilder( multilineStr ) );

public static List<string> ToLines( string multilineStrFmt, params object?[] args ) =>
   ToLines( string.Format( multilineStrFmt, args ) );

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

public static List<string> ToLines( StringBuilder multilineBld )
{
   var list = new List<string>();
   var sb = new StringBuilder();
   for ( int i=0; i<multilineBld.Length; i++ ) {
      char ch = multilineBld[ i ];
      if ( ch == '\n' ) {
         list.Add( sb.ToString() );
         sb.Clear();
      }
      else
         if ( ch != '\r' )
            sb.Append( ch );
   }
   if ( sb.Length > 0 )
      list.Add( sb.ToString() );
   return list;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // text

} // user_rtx (.wasm)
