//using System;
//using System.Linq.Expressions;
//using System.Reflection;

//namespace Modules.Library.Domain.Utils;

//internal static class OrderBaseUtil<T>
//{
//    internal static void SetItemOrder(IEnumerable<T> items, ushort lastOrder, ushort newOrder, Expression<Func<T, ushort>> getOrderPropertyExpr, Func<T, ushort> getOrderPropertyFunc)
//    {
//        if (IsOrderOutOfRange(items, getOrderPropertyFunc, lastOrder)) throw new ArgumentOutOfRangeException(nameof(lastOrder));
//        if (IsOrderOutOfRange(items, getOrderPropertyFunc, newOrder)) throw new ArgumentOutOfRangeException(nameof(newOrder));
//        var minOrder = Math.Min(lastOrder, newOrder);
//        var maxOrder = Math.Max(lastOrder, newOrder);
//        var orderedItems = OrderedItemsInsideRange(items, getOrderPropertyFunc, minOrder, maxOrder).ToList();
//        var targetItem = orderedItems.First(q => getOrderPropertyFunc(q) == newOrder);
//        bool seen = false;
//        for (int i = 0; i < orderedItems.Count; i++)
//        {
//            if (object.ReferenceEquals(orderedItems[i], targetItem))
//            {
//                orderedItems[i].SetPropertyValue(getOrderPropertyExpr, newOrder); //orderedItems[i].Order = newOrder;
//                seen = true;
//            }
//            //else if (seen && items[i].Order <= newOrder)
//            else if (seen && getOrderPropertyFunc(orderedItems[i]) <= newOrder)
//            {
//                orderedItems[i].SetPropertyValue(getOrderPropertyExpr, (ushort)(newOrder - 1)); //orderedItems[i].Order--; // move it left
//            }
//            //else if (items[i].Order >= newOrder)
//            else if (getOrderPropertyFunc(orderedItems[i]) >= newOrder)
//            {
//                orderedItems[i].SetPropertyValue(getOrderPropertyExpr, (ushort)(newOrder + 1)); //orderedItems[i].Order++; // move it right
//            }
//        }
//    }

//    private static IEnumerable<T> OrderedItemsInsideRange(IEnumerable<T> items, Func<T, ushort> getOrderPropertyFunc, ushort min, ushort max) =>
//        items.Where(q => getOrderPropertyFunc(q) >= min && getOrderPropertyFunc(q) <= max);

//    internal static bool IsOrderOutOfRange(IEnumerable<T> items, Func<T, ushort> getOrderPropertyFunc, ushort order) => order < 0 ||
//        order > items.Select(getOrderPropertyFunc).DefaultIfEmpty((ushort)0).Max();
//}

//public static class LambdaExtensions
//{
//    public static void SetPropertyValue<T, TValue>(this T target, Expression<Func<T, TValue>> memberLamda, TValue value)
//    {
//        var memberSelectorExpression = memberLamda.Body as MemberExpression;
//        if (memberSelectorExpression != null)
//        {
//            var property = memberSelectorExpression.Member as PropertyInfo;
//            if (property != null)
//            {
//                property.SetValue(target, value, null);
//            }
//        }
//    }
//}
