using Avalonia;
using Avalonia.Controls;
using ClaudeDo.Ui.Services;

namespace ClaudeDo.Ui.Views.Controls;

public partial class OperationIndicator : UserControl
{
    public static readonly StyledProperty<OperationStatus?> StatusProperty =
        AvaloniaProperty.Register<OperationIndicator, OperationStatus?>(nameof(Status));

    public OperationStatus? Status
    {
        get => GetValue(StatusProperty);
        set => SetValue(StatusProperty, value);
    }

    public OperationIndicator()
    {
        InitializeComponent();
        IsVisible = false;
    }

    /// A null `Status` leaves the inner panel's DataContext null, so its `IsVisible="{Binding
    /// ShowIndicator}"` can't resolve and falls back to the property default — *visible*. That
    /// renders a label-less spinner plus the stalled hint forever. Collapse the whole control
    /// instead. Deliberately a direct set, not a binding: no call site binds `IsVisible` on the
    /// indicator, and a local value would win over one anyway.
    protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
    {
        base.OnPropertyChanged(change);
        if (change.Property == StatusProperty)
            IsVisible = change.GetNewValue<OperationStatus?>() is not null;
    }
}
