@using System.Linq
@using System.Collections
@using Radzen
@using Radzen.Blazor.Rendering
@using Microsoft.AspNetCore.Components.Forms
@typeparam TValue
@inherits FormComponent<TValue>
@implements IRadzenChipList

@if (Items != null)
{
    <CascadingValue Value=this>
        @Items
    </CascadingValue>
}
@if (Visible)
{
    <div @ref="@Element" style="@($"{Style};{WrapStyle}")" @attributes="Attributes" class="@GetCssClass()" id="@GetId()"
         role="listbox"
         aria-multiselectable="@Multiple"
         aria-disabled="@(Disabled ? "true" : "false")"
         tabindex="@(Disabled ? "-1" : $"{TabIndex}")"
         @onfocus="OnFocus" @onblur="OnBlur"
         @onkeydown="OnKeyPress" @onkeydown:preventDefault=preventKeyPress @onkeydown:stopPropagation>
        @foreach (var item in allItems)
        {
            var selected = IsSelected(item);
            var hasTemplate = item.Template != null || Template != null;
            <div class="@ClassList.Create("rz-chip-list-item").Add("rz-state-focused", IsFocused(item) && focused).ToString()"
                 role="option"
                 tabindex="-1"
                 aria-selected="@(selected ? "true" : "false")"
                 aria-disabled="@(Disabled || item.Disabled ? "true" : "false")"
                 @onclick="@(args => SelectItem(item))">
                @if (hasTemplate)
                {
                    <RadzenChip Text="@item.Text"
                                Icon="@item.Icon"
                                ChipStyle="@(item.ChipStyle ?? ChipStyle)"
                                Variant="@(item.Variant ?? Variant)"
                                Shade="@Shade"
                                Size="@Size"
                                TabIndex="-1"
                                Selected="@selected"
                                Disabled="@(Disabled || item.Disabled)"
                                RemoveChipTitle="@RemoveChipTitle"
                                Close="@GetRemoveCallback(item)">
                        @if (item.Template != null)
                        {
                            @item.Template(item)
                        }
                        else
                        {
                            @Template!(item)
                        }
                    </RadzenChip>
                }
                else
                {
                    <RadzenChip Text="@item.Text"
                                Icon="@item.Icon"
                                ChipStyle="@(item.ChipStyle ?? ChipStyle)"
                                Variant="@(item.Variant ?? Variant)"
                                Shade="@Shade"
                                Size="@Size"
                                TabIndex="-1"
                                Selected="@selected"
                                Disabled="@(Disabled || item.Disabled)"
                                RemoveChipTitle="@RemoveChipTitle"
                                Close="@GetRemoveCallback(item)" />
                }
            </div>
        }
    </div>
}
