@* Static AB CIP address builder: tag name + optional element index → tag[idx] or tag *@

<div class="row g-3">
    <div class="col-md-6">
        <label class="form-label">Tag name</label>
        <input type="text" class="form-control form-control-sm mono" placeholder="Program:Main.MyTag"
               @bind="_tagName" @bind:after="OnChangedAsync" />
        <div class="form-text">Use dot notation for nested tags or UDT members.</div>
    </div>
    <div class="col-md-3">
        <label class="form-label">Element index (optional)</label>
        <input type="number" class="form-control form-control-sm" min="0"
               @bind="_elementIndex" @bind:after="OnChangedAsync" />
        <div class="form-text">Leave 0 for non-array tags (no index appended).</div>
    </div>
    <div class="col-md-3 d-flex align-items-end">
        <div class="form-check mb-2">
            <input type="checkbox" class="form-check-input" id="abCipUseIdx"
                   @bind="_useIndex" @bind:after="OnChangedAsync" />
            <label class="form-check-label" for="abCipUseIdx">Append index</label>
        </div>
    </div>
</div>

<div class="mt-3">
    <span class="text-muted small">Result:</span>
    <code class="mono ms-2">@_built</code>
</div>

@code {
    [Parameter] public string CurrentAddress { get; set; } = "";
    [Parameter] public EventCallback<string> CurrentAddressChanged { get; set; }

    private string _tagName = "";
    private int _elementIndex = 0;
    private bool _useIndex = false;
    private string _built = "";

    protected override void OnInitialized()
    {
        _built = Build();
        _ = CurrentAddressChanged.InvokeAsync(_built);
    }

    private async Task OnChangedAsync()
    {
        _built = Build();
        await CurrentAddressChanged.InvokeAsync(_built);
    }

    private string Build()
    {
        if (string.IsNullOrWhiteSpace(_tagName))
            return "";
        return _useIndex ? $"{_tagName}[{_elementIndex}]" : _tagName;
    }
}
