@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol
<li>
    <span style="cursor: @(Node.HasChildren ? "pointer" : "default");" @onclick="() => OnToggle.InvokeAsync(Node)">
        @if (Node.HasChildren)
        {
            <span>@(Node.Expanded ? "▼" : "▶")</span>
        }
        else
        {
            <span style="display:inline-block; width:1em;">&nbsp;</span>
        }
    </span>

    @if (Node.NodeClass == BrowseNodeClass.Variable)
    {
        <a href="javascript:void(0)"
           class="@(Node.NodeId == SelectedNodeId ? "fw-bold text-primary" : "")"
           @onclick="() => OnSelect.InvokeAsync(Node)"
           @ondblclick="() => OnSelect.InvokeAsync(Node)">
            @Node.DisplayName <small class="text-muted">(@Node.NodeId)</small>
        </a>
    }
    else
    {
        <span class="text-muted">@Node.DisplayName</span>
    }

    @if (Node.Loading)
    {
        <em class="ms-2 text-muted">loading…</em>
    }

    @if (Node.Expanded && Node.Children is not null)
    {
        <ul class="list-unstyled ms-4">
            @foreach (var child in Node.Children)
            {
                <TreeRow Node="child" OnToggle="OnToggle" OnSelect="OnSelect" SelectedNodeId="@SelectedNodeId" />
            }
            @if (Node.Truncated)
            {
                <li><small class="text-warning">Results truncated — use manual entry if your tag isn't listed.</small></li>
            }
        </ul>
    }
</li>

@code {
    [Parameter] public NodeBrowserDialog.TreeNode Node { get; set; } = default!;
    [Parameter] public EventCallback<NodeBrowserDialog.TreeNode> OnToggle { get; set; }
    [Parameter] public EventCallback<NodeBrowserDialog.TreeNode> OnSelect { get; set; }
    [Parameter] public string? SelectedNodeId { get; set; }
}
