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

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

//**************************************************************************************************
partial class HString {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static int MinLength(IEnumerable<string> collection)
{
   int index; return GetMinLength(collection,out index);
}

public static int MaxLength(IEnumerable<string> collection)
{
   int index; return GetMaxLength(collection,out index);
}

public static int MinLength(params string[] array)
{
   return MinLength((IEnumerable<string>)array);
}

public static int MaxLength(params string[] array)
{
   return MaxLength((IEnumerable<string>)array);
}

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

public static int GetMinLength(IEnumerable<string> collection,out int index)
{
   return GetMinOrMaxLength(collection,HMinOrMax.Min,out index);
}

public static int GetMaxLength(IEnumerable<string> collection,out int index)
{
   return GetMinOrMaxLength(collection,HMinOrMax.Max,out index);
}

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

public static int MinLengthIndex(IEnumerable<string> collection)
{
   int index; GetMinLength(collection,out index); return index;
}

public static int MaxLengthIndex(IEnumerable<string> collection)
{
   int index; GetMaxLength(collection,out index); return index;
}

public static int MinLengthIndex(params string[] array)
{
   return MinLengthIndex((IEnumerable<string>)array);
}

public static int MaxLengthIndex(params string[] array)
{
   return MaxLengthIndex((IEnumerable<string>)array);
}

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

public static int GetMinOrMaxLength( IEnumerable<string> collection,
                                     HMinOrMax minmax,
                                     out int index )
{
   HArgChecking.VerifyNotNull(collection);

   int result=0; index=-1;

   using (IEnumerator<string> enumerator=collection.GetEnumerator())
   {
      if (!enumerator.MoveNext())
         return result;
      int
         idx=0;
      do
      {
         string element=enumerator.Current;

         if (element!=null)
         {
            int len=element.Length;

            if ( index<0 ||
                 minmax==HMinOrMax.Min && len<result ||
                 minmax==HMinOrMax.Max && len>result )
            {
               result=len; index=idx;
            }
         }

         idx++;
      }
      while (enumerator.MoveNext());
   }

   return result;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HString
//**************************************************************************************************

} // CommonHelpers (.WASM)
