// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sample.Models;

namespace Sample
{
    internal partial class CatClientGetCatsAsyncCollectionResult : global::System.ClientModel.Primitives.AsyncCollectionResult
    {
        private readonly global::Sample.CatClient _client;
        private readonly string _myToken;
        private readonly global::System.ClientModel.Primitives.RequestOptions _options;

        public CatClientGetCatsAsyncCollectionResult(global::Sample.CatClient client, string myToken, global::System.ClientModel.Primitives.RequestOptions options)
        {
            _client = client;
            _myToken = myToken;
            _options = options;
        }

        public override async global::System.Collections.Generic.IAsyncEnumerable<global::System.ClientModel.ClientResult> GetRawPagesAsync()
        {
            global::System.ClientModel.Primitives.PipelineMessage message = _client.CreateGetCatsRequest(_myToken, _options);
            string nextToken = null;
            while (true)
            {
                global::System.ClientModel.ClientResult result = await this.GetNextResponseAsync(message).ConfigureAwait(false);
                yield return result;

                nextToken = ((global::Sample.Models.Page)result).NextPage;
                if (string.IsNullOrEmpty(nextToken))
                {
                    yield break;
                }
                message = _client.CreateGetCatsRequest(nextToken, _options);
            }
        }

        public override global::System.ClientModel.ContinuationToken GetContinuationToken(global::System.ClientModel.ClientResult page)
        {
            string nextPage = ((global::Sample.Models.Page)page).NextPage;
            if (!string.IsNullOrEmpty(nextPage))
            {
                return global::System.ClientModel.ContinuationToken.FromBytes(global::System.BinaryData.FromString(nextPage));
            }
            else
            {
                return null;
            }
        }

        private async global::System.Threading.Tasks.ValueTask<global::System.ClientModel.ClientResult> GetNextResponseAsync(global::System.ClientModel.Primitives.PipelineMessage message)
        {
            return global::System.ClientModel.ClientResult.FromResponse(await _client.Pipeline.ProcessMessageAsync(message, _options).ConfigureAwait(false));
        }
    }
}
