// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Text;
using System.Text.Json;
using Sample.Models;

namespace Sample
{
    public partial class DynamicModel
    {
        global::System.BinaryData global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.DynamicModel>.Write(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => this.PersistableModelWriteCore(options);

        void global::System.ClientModel.Primitives.IJsonModel<global::Sample.Models.DynamicModel>.Write(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            if (Patch.Contains("$"u8))
            {
                writer.WriteRawValue(Patch.GetJson("$"u8));
                return;
            }
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.

            writer.WriteStartObject();
            this.JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected virtual void JsonModelWriteCore(global::System.Text.Json.Utf8JsonWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.DynamicModel>)this).GetFormatFromOptions(options) : options.Format;
            if ((format != "J"))
            {
                throw new global::System.FormatException($"The model {nameof(global::Sample.Models.DynamicModel)} does not support writing '{format}' format.");
            }
#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
            if (Patch.Contains("$.propertyWithNestedArray"u8))
            {
                if (!Patch.IsRemoved("$.propertyWithNestedArray"u8))
                {
                    writer.WritePropertyName("propertyWithNestedArray"u8);
                    writer.WriteRawValue(Patch.GetJson("$.propertyWithNestedArray"u8));
                }
            }
            else if (global::Sample.Optional.IsCollectionDefined(PropertyWithNestedArray))
            {
                writer.WritePropertyName("propertyWithNestedArray"u8);
                writer.WriteStartArray();
                for (int i = 0; (i < PropertyWithNestedArray.Count); i++)
                {
                    if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}]")))
                    {
                        continue;
                    }
                    if ((PropertyWithNestedArray[i] == null))
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStartArray();
                    for (int i0 = 0; (i0 < PropertyWithNestedArray[i].Count); i0++)
                    {
                        if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}]")))
                        {
                            continue;
                        }
                        if ((PropertyWithNestedArray[i][i0] == null))
                        {
                            writer.WriteNullValue();
                            continue;
                        }
                        writer.WriteStartArray();
                        for (int i1 = 0; (i1 < PropertyWithNestedArray[i][i0].Count); i1++)
                        {
                            if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}][{i1}]")))
                            {
                                continue;
                            }
                            if ((PropertyWithNestedArray[i][i0][i1] == null))
                            {
                                writer.WriteNullValue();
                                continue;
                            }
                            writer.WriteStartObject();
#if NET8_0_OR_GREATER
                            global::System.Span<byte> buffer = stackalloc byte[256];
#endif
                            foreach (var item in PropertyWithNestedArray[i][i0][i1])
                            {
#if NET8_0_OR_GREATER
                                int bytesWritten = global::System.Text.Encoding.UTF8.GetBytes(item.Key.AsSpan(), buffer);
                                bool patchContains = (bytesWritten == 256) ? Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}][{i1}]"), global::System.Text.Encoding.UTF8.GetBytes(item.Key)) : Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}][{i1}]"), buffer.Slice(0, bytesWritten));
#else
                                bool patchContains = Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}][{i1}]"), global::System.Text.Encoding.UTF8.GetBytes(item.Key));
#endif
                                if (!patchContains)
                                {
                                    writer.WritePropertyName(item.Key);
                                    if ((item.Value == null))
                                    {
                                        writer.WriteNullValue();
                                        continue;
                                    }
                                    writer.WriteStringValue(item.Value);
                                }
                            }

                            Patch.WriteTo(writer, global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}][{i1}]"));
                            writer.WriteEndObject();
                        }
                        Patch.WriteTo(writer, global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}][{i0}]"));
                        writer.WriteEndArray();
                    }
                    Patch.WriteTo(writer, global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedArray[{i}]"));
                    writer.WriteEndArray();
                }
                Patch.WriteTo(writer, "$.propertyWithNestedArray"u8);
                writer.WriteEndArray();
            }

            Patch.WriteTo(writer);
#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
        }
    }
}
