using ZB.MOM.WW.OtOpcUa.Core.Abstractions;

namespace ZB.MOM.WW.OtOpcUa.Core.Hosting;

/// <summary>
///     Adapts the existing <see cref="DriverFactoryRegistry"/> (v1 surface, still the
///     concrete singleton every driver assembly registers itself against) to the v2
///     <see cref="IDriverFactory"/> abstraction consumed by Runtime. The fused Host binds
///     this in DI once each <c>Driver.*.Register(registry)</c> call has completed.
/// </summary>
public sealed class DriverFactoryRegistryAdapter : IDriverFactory
{
    private readonly DriverFactoryRegistry _registry;

    /// <summary>Initializes the adapter with the underlying registry.</summary>
    /// <param name="registry">The driver factory registry to adapt.</param>
    public DriverFactoryRegistryAdapter(DriverFactoryRegistry registry)
    {
        ArgumentNullException.ThrowIfNull(registry);
        _registry = registry;
    }

    /// <inheritdoc />
    public IDriver? TryCreate(string driverType, string driverInstanceId, string driverConfigJson)
    {
        var factory = _registry.TryGet(driverType);
        return factory?.Invoke(driverInstanceId, driverConfigJson);
    }

    /// <inheritdoc />
    public IReadOnlyCollection<string> SupportedTypes => _registry.RegisteredTypes;
}
