namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Uns;

/// <summary>
/// Pure helpers extracted from <see cref="VirtualTagModal"/> to support unit testing of state
/// logic that is otherwise embedded in Razor component lifecycle methods.
/// </summary>
internal static class VirtualTagModalHelpers
{
    /// <summary>
    /// Resolves the display label for the given <paramref name="selectedId"/> from the supplied
    /// dropdown <paramref name="options"/> list.
    /// </summary>
    /// <param name="options">
    /// The current in-memory options, each as an <c>(Id, Display)</c> pair. This list MUST include
    /// the newly-created script entry BEFORE <c>selectedId</c> is set on the form; otherwise the
    /// rendered <c>&lt;select&gt;</c> will have no matching <c>&lt;option&gt;</c> and show a blank
    /// label.
    /// </param>
    /// <param name="selectedId">The id of the currently selected script (may be null or empty).</param>
    /// <returns>
    /// The display label for the selected id, or <c>null</c> when <paramref name="selectedId"/> is
    /// empty or is not present in <paramref name="options"/>.
    /// </returns>
    internal static string? ResolveScriptLabel(
        IEnumerable<(string Id, string Display)> options,
        string? selectedId)
    {
        if (string.IsNullOrEmpty(selectedId))
            return null;

        foreach (var (id, display) in options)
        {
            if (id == selectedId)
                return display;
        }

        return null;
    }
}
