using Hcs.Broker.Api.Request;
using Hcs.Broker.Api.Request.Exception;
using Hcs.Broker.Api.Request.Nsi;
using Hcs.Service.Async.Nsi;

namespace Hcs.Broker.Api
{
    /// <inheritdoc cref="INsiApi"/>
    public class NsiApi(Client client) : ApiBase(client), INsiApi
    {
        /// <inheritdoc cref="INsiApi"/>
        public async Task<IEnumerable<NsiItemType>> ExportDataProviderNsiItemAsync(exportDataProviderNsiItemRequestRegistryNumber registryNumber, CancellationToken token = default)
        {
            try
            {
                var request = new ExportDataProviderNsiItemRequest(client);
                return await request.ExecuteAsync(registryNumber, token);
            }
            catch (NoResultsRemoteException)
            {
                return [];
            }
        }

        /// <inheritdoc cref="INsiApi"/>
        public async Task<string> RequestExportDataProviderNsiItemAsync(exportDataProviderNsiItemRequestRegistryNumber registryNumber, CancellationToken token = default)
        {
            var request = new ExportDataProviderNsiItemRequest(client);
            return await request.SendAsync(registryNumber, token);
        }

        /// <inheritdoc cref="INsiApi"/>
        public async Task<RequestSingleResult<NsiItemType>> GetExportDataProviderNsiItemResultAsync(string messageGuid)
        {
            var request = new ExportDataProviderNsiItemRequest(client);
            return await request.GetResultAsync(messageGuid);
        }
    }
}
