using AIStudio.Settings;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
///
/// Configuration component for selecting many values from a list.
///
/// The type of the value to select.
public partial class ConfigurationMultiSelect : ConfigurationBaseCore
{
///
/// The data to select from.
///
[Parameter]
public IEnumerable> Data { get; set; } = [];
///
/// The selected values.
///
[Parameter]
public Func> SelectedValues { get; set; } = () => [];
///
/// An action that is called when the selection changes.
///
[Parameter]
public Action> SelectionUpdate { get; set; } = _ => { };
///
/// Determines whether a specific item is locked by a configuration plugin.
///
[Parameter]
public Func IsItemLocked { get; set; } = _ => false;
[Parameter]
public string? EmptySelectionText { get; set; }
[Parameter]
public string? SingleSelectionText { get; set; }
[Parameter]
public string? MultipleSelectionText { get; set; }
#region Overrides of ConfigurationBase
///
protected override bool Stretch => true;
///
protected override Variant Variant => Variant.Outlined;
///
protected override string Label => this.OptionDescription;
#endregion
private async Task OptionChanged(IEnumerable? updatedValues)
{
if(updatedValues is null)
this.SelectionUpdate([]);
else
this.SelectionUpdate(updatedValues.Where(n => n is not null).ToHashSet()!);
await this.SettingsManager.StoreSettings();
await this.InformAboutChange();
}
private string GetMultiSelectionText(List? selectedValues)
{
if(selectedValues is null || selectedValues.Count == 0)
return this.EmptySelectionText ?? T("No items selected.");
if(selectedValues.Count == 1)
return this.SingleSelectionText ?? T("You have selected 1 item.");
return string.Format(this.MultipleSelectionText ?? T("You have selected {0} items."), selectedValues.Count);
}
private bool IsLockedValue(TData value) => this.IsItemLocked(value);
private string LockedTooltip() =>
this.T(
"This feature is managed by your organization and has therefore been disabled.",
typeof(ConfigurationBase).Namespace,
nameof(ConfigurationBase));
}