using Connected.Annotations;
using Microsoft.AspNetCore.Components;

namespace Connected.Components;

public abstract class BindableItemsControlBase<TChildComponent, TData> : ItemsControlBase<TChildComponent>
	 where TChildComponent : UIComponent
{

	/// <summary>
	/// Items Collection - For databinding usage
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.Data)]
	public IEnumerable<TData> ItemsSource { get; set; }

	/// <summary>
	/// Template for each Item in ItemsSource collection
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.General.Appearance)]
	public RenderFragment<TData> ItemTemplate { get; set; }

	/// <summary>
	/// Gets the Selected Item from ItemsSource, or Selected TChildComponent, when it's null
	/// </summary>
	public object SelectedItem
	{
		get => ItemsSource == null ? Items[SelectedIndex] : ItemsSource.ElementAtOrDefault(SelectedIndex);
	}
}
