using System;
using System.Collections.Generic;
using System.Linq;

namespace Replikanto.Util
{
	public static class ListExtensions
	{
		public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
		{
			ListExtensions.<>c__DisplayClass0_0<T> CS$<>8__locals1 = new ListExtensions.<>c__DisplayClass0_0<T>();
			CS$<>8__locals1.chunkSize = chunkSize;
			return source.Select(new Func<T, int, <>f__AnonymousType0<int, T>>(ListExtensions.<>c__0<T>.<>9.method_0)).GroupBy(new Func<<>f__AnonymousType0<int, T>, int>(CS$<>8__locals1.method_0)).Select(new Func<IGrouping<int, <>f__AnonymousType0<int, T>>, List<T>>(ListExtensions.<>c__0<T>.<>9.method_1))
				.ToList<List<T>>();
		}

		public static List<List<T>> RearrangeBy<T>(this List<T> source, int capacity)
		{
			List<T> list = source.ToList<T>();
			List<List<T>> list2 = new List<List<T>>(capacity);
			for (int i = 0; i < capacity; i++)
			{
				list2.Add(new List<T>());
			}
			for (int j = capacity - 1; j >= 0; j--)
			{
				int num = (int)Delegate376.smethod_0((double)list.Count / (double)(j + 1), Delegate376.delegate376_0);
				for (int k = 0; k < num; k++)
				{
					if (list.Count > 0)
					{
						T t = list.Last<T>();
						if (t != null)
						{
							list2[j].Add(t);
							list.Remove(t);
						}
					}
				}
			}
			return list2;
		}
	}
}
