using System.Globalization;
using Avalonia.Data.Converters;
using CommunityToolkit.Mvvm.ComponentModel;

namespace InABox.Avalonia.Converters;

public abstract class AbstractConverter<TIn, TOut> : ObservableObject, IValueConverter
{
    
    protected abstract TOut? Convert(TIn? value, object? parameter = null);

    protected virtual TIn? Deconvert(TOut? value, object? parameter = null)
    {
        return default;
    }
    
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var typed = value is TIn tin ? tin : default;
        return Convert(typed, parameter);
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var typed = value is TOut tout ? tout : default;
        return Deconvert(typed, parameter);
    }
    
}