using MediatR;
using Microsoft.Extensions.DependencyInjection;

namespace Leanetec.EConnect.Client;

/// <summary>
/// Provides a lazily initialized <see cref="IServiceProvider"/> for accessing the EConnect client services,
/// including an <see cref="IMediator"/> instance for sending and publishing messages.
/// </summary>
public static class Client
{
    /// <summary>
    /// Lazily initializes the <see cref="IServiceProvider"/> that registers and builds the EConnect client services.
    /// </summary>
    private static readonly Lazy<IServiceProvider> _serviceProvider = new(() =>
    {
        var services = new ServiceCollection();
        services.AddClientServices();
        return services.BuildServiceProvider();
    });

    /// <summary>
    /// Gets the initialized <see cref="IServiceProvider"/> that provides access to registered EConnect client services.
    /// </summary>
    public static IServiceProvider Provider => _serviceProvider.Value;

    /// <summary>
    /// Gets the <see cref="IMediator"/> instance used for sending commands, queries, and publishing events within the EConnect client.
    /// </summary>
    public static IMediator Mediator => Provider.GetRequiredService<IMediator>();
}