using IdokladSdk;
using Idoklad.Credentials;

namespace Idoklad.Client;

/// <summary>
/// Scoped accessor that resolves the credentials for the current request and lazily builds a
/// single <see cref="DokladApi"/> shared by all services handling that request.
/// </summary>
public sealed class IdokladApiAccessor
{
    private readonly RequestCredentialsProvider _credentialsProvider;
    private readonly DokladApiFactory _factory;
    private DokladApi? _api;

    public IdokladApiAccessor(RequestCredentialsProvider credentialsProvider, DokladApiFactory factory)
    {
        _credentialsProvider = credentialsProvider;
        _factory = factory;
    }

    public DokladApi Api => _api ??= _factory.Create(_credentialsProvider.Resolve());
}
