// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;

namespace OpenAI.Chat
{
    internal partial class ChatClientGetChatCompletionsCollectionResult : CollectionResult
    {
        private readonly ChatClient _client;
        private readonly string _after;
        private readonly int? _limit;
        private readonly string _order;
        private readonly IDictionary<string, string> _metadata;
        private readonly string _model;
        private readonly RequestOptions _options;

        public ChatClientGetChatCompletionsCollectionResult(ChatClient client, string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options)
        {
            _client = client;
            _after = after;
            _limit = limit;
            _order = order;
            _metadata = metadata;
            _model = model;
            _options = options;
        }

        public override IEnumerable<ClientResult> GetRawPages()
        {
            PipelineMessage message = _client.CreateGetChatCompletionsRequest(_after, _limit, _order, _metadata, _model, _options);
            string nextToken = null;
            while (true)
            {
                ClientResult result = ClientResult.FromResponse(_client.Pipeline.ProcessMessage(message, _options));
                yield return result;

                nextToken = ((InternalChatCompletionList)result).LastId;
                if (nextToken == null)
                {
                    yield break;
                }
                message = _client.CreateGetChatCompletionsRequest(nextToken, _limit, _order, _metadata, _model, _options);
            }
        }

        public override ContinuationToken GetContinuationToken(ClientResult page)
        {
            string nextPage = ((InternalChatCompletionList)page).LastId;
            if (nextPage != null)
            {
                return ContinuationToken.FromBytes(BinaryData.FromString(nextPage));
            }
            else
            {
                return null;
            }
        }
    }
}
