@using System.Linq
@using Radzen.Blazor.Rendering

@inherits RadzenComponent

@if (Items != null)
{
    <CascadingValue Value=this>
        @Items
    </CascadingValue>
}
@if (Visible)
{
<div @ref="@Element" style=@Style @attributes="Attributes" class="@GetCssClass()" id="@GetId()"
    tabindex="0" @onkeydown="@((args) => OnKeyPress(args))" @onkeydown:preventDefault=preventKeyPress @onkeydown:stopPropagation=preventKeyPress>
    @for (var i = 0; i < items.Count; i++)
    {
        var item = items[i];
        if (!item.Visible)
            continue;

        var accordionId = $"{GetId()}-accordiontab-{items.IndexOf(item)}";
        <div @ref="@item.Element" id="@item.GetItemId()" @attributes="item.Attributes" class="@item.GetItemCssClass()" style="@item.Style" @onkeydown:stopPropagation="stopKeydownPropagation" @onkeydown="OnGuardKeyDown">
            <button type="button" @onclick="@((args) => SelectItem(item))" aria-label="@ItemAriaLabel(i, item)" title="@ItemTitle(i, item)"
                    id="@accordionId" aria-controls="@($"{accordionId}-content")" aria-expanded="@(item.GetSelected() ? "true" : "false")"
                    tabindex="@(item.GetSelected() ? "0" : "-1")">
                <span class="@ToggleIconClass(item)">keyboard_arrow_down</span>
                @if (!string.IsNullOrEmpty(item.Icon))
                {
            <i class="notranslate rzi" style="@(!string.IsNullOrEmpty(item.IconColor) ? $"color:{item.IconColor}" : null)">@item.Icon</i>
                }
                @if (item.Template != null)
                {
                    @item.Template
                }
                else if(!string.IsNullOrEmpty(item.Text))
                {
                    <span>@item.Text</span>
                }
            </button>
        </div>
        <Expander Expanded=@item.GetSelected()
                id="@($"{accordionId}-content")" aria-labelledby="@accordionId">
            <div class="rz-accordion-content" @onkeydown:stopPropagation="stopKeydownPropagation" @onkeydown="OnGuardKeyDown">
                @item.ChildContent
            </div>
        </Expander>
    }
</div>
}