// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Audio
{
    public partial class StreamingAudioTranscriptionTextDeltaUpdate : IJsonModel<StreamingAudioTranscriptionTextDeltaUpdate>
    {
        internal StreamingAudioTranscriptionTextDeltaUpdate() : this(StreamingAudioTranscriptionUpdateKind.TranscriptTextDelta, null, null, null)
        {
        }

        void IJsonModel<StreamingAudioTranscriptionTextDeltaUpdate>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(StreamingAudioTranscriptionTextDeltaUpdate)} does not support writing '{format}' format.");
            }
            base.JsonModelWriteCore(writer, options);
            if (_additionalBinaryDataProperties?.ContainsKey("delta") != true)
            {
                writer.WritePropertyName("delta"u8);
                writer.WriteStringValue(Delta);
            }
            if (Optional.IsCollectionDefined(TranscriptionTokenLogProbabilities) && _additionalBinaryDataProperties?.ContainsKey("logprobs") != true)
            {
                writer.WritePropertyName("logprobs"u8);
                writer.WriteStartArray();
                foreach (AudioTokenLogProbabilityDetails item in TranscriptionTokenLogProbabilities)
                {
                    writer.WriteObjectValue(item, options);
                }
                writer.WriteEndArray();
            }
        }

        StreamingAudioTranscriptionTextDeltaUpdate IJsonModel<StreamingAudioTranscriptionTextDeltaUpdate>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (StreamingAudioTranscriptionTextDeltaUpdate)JsonModelCreateCore(ref reader, options);

        protected override StreamingAudioTranscriptionUpdate JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(StreamingAudioTranscriptionTextDeltaUpdate)} does not support reading '{format}' format.");
            }
            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeStreamingAudioTranscriptionTextDeltaUpdate(document.RootElement, options);
        }

        internal static StreamingAudioTranscriptionTextDeltaUpdate DeserializeStreamingAudioTranscriptionTextDeltaUpdate(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            StreamingAudioTranscriptionUpdateKind kind = default;
            IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
            string delta = default;
            IReadOnlyList<AudioTokenLogProbabilityDetails> transcriptionTokenLogProbabilities = default;
            foreach (var prop in element.EnumerateObject())
            {
                if (prop.NameEquals("type"u8))
                {
                    kind = new StreamingAudioTranscriptionUpdateKind(prop.Value.GetString());
                    continue;
                }
                if (prop.NameEquals("delta"u8))
                {
                    delta = prop.Value.GetString();
                    continue;
                }
                if (prop.NameEquals("logprobs"u8))
                {
                    if (prop.Value.ValueKind == JsonValueKind.Null)
                    {
                        continue;
                    }
                    List<AudioTokenLogProbabilityDetails> array = new List<AudioTokenLogProbabilityDetails>();
                    foreach (var item in prop.Value.EnumerateArray())
                    {
                        array.Add(AudioTokenLogProbabilityDetails.DeserializeAudioTokenLogProbabilityDetails(item, options));
                    }
                    transcriptionTokenLogProbabilities = array;
                    continue;
                }
                // Plugin customization: remove options.Format != "W" check
                additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
            }
            return new StreamingAudioTranscriptionTextDeltaUpdate(kind, additionalBinaryDataProperties, delta, transcriptionTokenLogProbabilities ?? new ChangeTrackingList<AudioTokenLogProbabilityDetails>());
        }

        BinaryData IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
                default:
                    throw new FormatException($"The model {nameof(StreamingAudioTranscriptionTextDeltaUpdate)} does not support writing '{options.Format}' format.");
            }
        }

        StreamingAudioTranscriptionTextDeltaUpdate IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>.Create(BinaryData data, ModelReaderWriterOptions options) => (StreamingAudioTranscriptionTextDeltaUpdate)PersistableModelCreateCore(data, options);

        protected override StreamingAudioTranscriptionUpdate PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    using (JsonDocument document = JsonDocument.Parse(data))
                    {
                        return DeserializeStreamingAudioTranscriptionTextDeltaUpdate(document.RootElement, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(StreamingAudioTranscriptionTextDeltaUpdate)} does not support reading '{options.Format}' format.");
            }
        }

        string IPersistableModel<StreamingAudioTranscriptionTextDeltaUpdate>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
    }
}
