@using Radzen
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.JSInterop
@typeparam TValue
@{var itemArgs = DropDown?.ItemAttributes(this); }
@if (itemArgs?.Visible == true)
{
Disabled = itemArgs.Disabled;
@if (DropDown?.Multiple == true)
{
    <li class="@GetComponentCssClass("rz-multiselect")"
        aria-label="@(DropDown?.GetItemOrValueFromProperty(Item!, DropDown?.TextProperty ?? string.Empty) ?? "")"
        @onmousedown:preventDefault @onmousedown="args=>SelectItem(args,false)"
        @onclick:preventDefault @onclick="args=>SelectItem(args,true)" 
        @attributes="@(itemArgs?.Attributes.Any() == true ? itemArgs.Attributes : Attributes)">
        <div class="rz-chkbox ">
            <div class="@(DropDown?.IsSelected(Item) == true ? "notranslate rz-chkbox-box rz-state-active" : "notranslate rz-chkbox-box") @(Disabled ? " rz-state-disabled  " : "")">
                <span class="@(DropDown?.IsSelected(Item) == true ? "notranslate rz-chkbox-icon rzi rzi-check" : "notranslate rz-chkbox-icon")"></span>
            </div>
        </div>
        <span class="rz-multiselect-item-content">
            @if (DropDown?.Template != null)
            {
                @DropDown.Template(Item)
            }
            else
            {
                @(DropDown?.GetItemOrValueFromProperty(Item!, DropDown?.TextProperty ?? string.Empty) ?? "")
            }
        </span>
    </li>
}
else
{
    <li role="option" class="@GetComponentCssClass("rz-dropdown")" aria-label="@(DropDown?.GetItemOrValueFromProperty(Item, DropDown?.TextProperty ?? "") ?? "")"
    @onmousedown:preventDefault @onmousedown="args=>SelectItem(args,false)"
    @onclick:preventDefault @onclick="args=>SelectItem(args,true)"
    @attributes="@(itemArgs?.Attributes.Any() == true ? itemArgs.Attributes : Attributes)">
        <span>
            @if (DropDown?.Template != null)
            {
                @DropDown.Template(Item)
            }
            else
            {
                @(DropDown?.GetItemOrValueFromProperty(Item!, DropDown?.TextProperty ?? string.Empty) ?? "")
            }
       </span>
    </li>
}
}
@code {
    [Parameter(CaptureUnmatchedValues = true)]
    public IReadOnlyDictionary<string, object> Attributes { get; set; } = new Dictionary<string, object>();

    [Parameter]
    public RadzenDropDown<TValue> DropDown { get; set; } = default!;

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public object Item { get; set; } = default!;

    async Task SelectItem(MouseEventArgs args,bool isclick)
    {
        var dropDown = DropDown;
        if (dropDown == null)
        {
            return;
        }

        var condition = dropDown.LoadData.HasDelegate && !string.IsNullOrEmpty(dropDown.searchText);

        if (!Disabled && isclick ? !condition : condition)
        {
            await dropDown.OnSelectItemInternal(Item);
        }
    }

    protected string GetComponentCssClass(string prefix)
    {
        var dropDown = DropDown;
        string result = $"{prefix}-item ";
        if (Disabled)
            result += "rz-state-disabled ";
        else if (dropDown != null && dropDown.IsSelected(Item))
            result += "rz-state-highlight ";

        return result;
    }
}