using System.Collections.Generic;
using System.Linq;
using SGModule.Common.Helper;
using SGModule.Common.Interface;

namespace SGModule.Common.Extensions {
    public static class ListExtensions {
        public static T Random<T>(this IEnumerable<T> items, T fallback = default) where T : class, IWeighted {
            if (items == null) {
                return fallback;
            }

            var list = items as IList<T> ?? items.ToList();

            if (list.Count == 0) {
                return fallback;
            }

            return RandomHelper.RandomByWeight(list, fallback);
        }
    }
}
