namespace InABox.Avalonia.Converters;


public abstract class AbstractCalculatorFunction<TValue, TType> where TType : struct, IComparable
{
    
    protected abstract TValue Calculate(TValue current, TValue next, TType type) ;
    public TValue Calculate(TValue[] value, TType type)
    {
        if (!value.Any())
            return default;
        var result = value.First();
        result = value.Skip(1).Aggregate(result, (current, next) => Calculate(current, next, type));
        return result;
    }
    
}

public abstract class AbstractCalculator<TValue,TFunction, TType> : AbstractMultiConverter<TValue,TValue> 
    where TFunction : AbstractCalculatorFunction<TValue,TType>, new()
    where TType : struct, IComparable
{

    public TValue Default { get; private set; }
    
    public TType Type { get; set; }
    
    public TFunction Function { get; private set; }

    protected AbstractCalculator()
    {
        Default = default;
        Function = new TFunction();
    }
    
    protected override TValue Convert(IEnumerable<TValue> value, object parameter = null)
    {
        var enumerable = value as TValue[] ?? value?.ToArray() ?? new TValue[] { };
        if (Function == null || !enumerable.Any())
            return Default;

        var result = Function.Calculate(enumerable, Type);
        return result;
    }
}
