namespace ZB.MOM.WW.OtOpcUa.Commons.Types;

/// <summary>
/// Logical cluster node identifier — typically the host name configured on a fused
/// <c>OtOpcUa.Host</c> instance. NOT to be confused with OPC UA <c>NodeId</c> from the
/// Opc.Ua.Core stack.
/// </summary>
public readonly record struct NodeId(string Value)
{
    /// <inheritdoc />
    public override string ToString() => Value;

    /// <summary>Parses a string into a NodeId.</summary>
    /// <param name="s">The string to parse.</param>
    /// <returns>A new NodeId instance.</returns>
    /// <exception cref="ArgumentException">Thrown when the string is null, empty, or whitespace.</exception>
    public static NodeId Parse(string s) =>
        string.IsNullOrWhiteSpace(s)
            ? throw new ArgumentException("NodeId value cannot be empty.", nameof(s))
            : new NodeId(s);

    /// <summary>Attempts to parse a string into a NodeId.</summary>
    /// <param name="s">The string to parse.</param>
    /// <param name="id">The parsed NodeId if successful.</param>
    /// <returns>True if the parse succeeded; otherwise false.</returns>
    public static bool TryParse(string? s, out NodeId id)
    {
        if (!string.IsNullOrWhiteSpace(s)) { id = new NodeId(s); return true; }
        id = default; return false;
    }
}
