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

/// <summary>
/// Late-binding adapter that holds an inner <see cref="IServiceLevelPublisher"/> reference
/// swappable at runtime. Mirrors <see cref="DeferredAddressSpaceSink"/>: Akka actors resolve
/// the publisher at DI time, but the production <c>SdkServiceLevelPublisher</c> only exists
/// after <c>StandardServer.Start</c>. The Host's hosted service swaps the inner once the SDK
/// is up; until then writes route through <see cref="NullServiceLevelPublisher"/>.
/// </summary>
public sealed class DeferredServiceLevelPublisher : IServiceLevelPublisher
{
    private volatile IServiceLevelPublisher _inner = NullServiceLevelPublisher.Instance;

    /// <summary>Swap the underlying publisher. Pass null to revert to the Null no-op.</summary>
    /// <param name="inner">The publisher implementation to use, or null to use the null publisher.</param>
    public void SetInner(IServiceLevelPublisher? inner) =>
        _inner = inner ?? NullServiceLevelPublisher.Instance;

    /// <inheritdoc />
    public void Publish(byte serviceLevel) => _inner.Publish(serviceLevel);
}
