using Hcs.Client.Internal;
using Hcs.Service.Async.Nsi;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Hcs.Client.Api.Request.Nsi
{
    internal class ExportDataProviderNsiItemRequest(ClientBase client) : NsiRequestBase(client)
    {
        internal async Task<IEnumerable<NsiItemType>> ExecuteAsync(exportDataProviderNsiItemRequestRegistryNumber registryNumber, CancellationToken token)
        {
            // http://open-gkh.ru/Nsi/exportDataProviderNsiItemRequest.html
            var request = new exportDataProviderNsiItemRequest
            {
                Id = Constants.SIGNED_XML_ELEMENT_ID,
                version = "10.0.1.2",
                RegistryNumber = registryNumber
            };

            var result = await SendAndWaitResultAsync(request, async asyncClient =>
            {
                var response = await asyncClient.exportDataProviderNsiItemAsync(CreateRequestHeader(), request);
                return response.AckRequest.Ack;
            }, token);

            return result.Items.OfType<NsiItemType>();
        }
    }
}
