using Microsoft.Extensions.Options;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;

namespace ZB.MOM.WW.ScadaBridge.Host;

/// <summary>
/// Binds <see cref="INodeIdentityProvider"/> to <see cref="NodeOptions.NodeName"/>.
/// Empty or whitespace values are normalised to <c>null</c>; otherwise the value
/// is returned trimmed.
/// </summary>
internal sealed class NodeIdentityProvider : INodeIdentityProvider
{
    /// <inheritdoc />
    public string? NodeName { get; }

    /// <summary>
    /// Initializes a new <see cref="NodeIdentityProvider"/> from the given node options.
    /// </summary>
    /// <param name="nodeOptions">Node options whose <see cref="NodeOptions.NodeName"/> is normalised.</param>
    public NodeIdentityProvider(IOptions<NodeOptions> nodeOptions)
    {
        var configured = nodeOptions.Value.NodeName;
        NodeName = string.IsNullOrWhiteSpace(configured) ? null : configured.Trim();
    }
}
