// <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 ((global::Sample.Optional.IsCollectionDefined(PropertyWithNestedDictionary) && !Patch.Contains("$.propertyWithNestedDictionary"u8)))
            {
                writer.WritePropertyName("propertyWithNestedDictionary"u8);
                writer.WriteStartObject();
#if NET8_0_OR_GREATER
                global::System.Span<byte> buffer = stackalloc byte[256];
#endif
                foreach (var item in PropertyWithNestedDictionary)
                {
#if NET8_0_OR_GREATER
                    int bytesWritten = global::System.Text.Encoding.UTF8.GetBytes(item.Key.AsSpan(), buffer);
                    bool patchContains = (bytesWritten == 256) ? Patch.Contains("$.propertyWithNestedDictionary"u8, global::System.Text.Encoding.UTF8.GetBytes(item.Key)) : Patch.Contains("$.propertyWithNestedDictionary"u8, buffer.Slice(0, bytesWritten));
#else
                    bool patchContains = Patch.Contains("$.propertyWithNestedDictionary"u8, global::System.Text.Encoding.UTF8.GetBytes(item.Key));
#endif
                    if (!patchContains)
                    {
                        writer.WritePropertyName(item.Key);
                        if ((item.Value == null))
                        {
                            writer.WriteNullValue();
                            continue;
                        }
                        writer.WriteStartObject();
#if NET8_0_OR_GREATER
                        global::System.Span<byte> buffer0 = stackalloc byte[256];
#endif
                        foreach (var item0 in item.Value)
                        {
#if NET8_0_OR_GREATER
                            int bytesWritten0 = global::System.Text.Encoding.UTF8.GetBytes(item0.Key.AsSpan(), buffer0);
                            bool patchContains0 = (bytesWritten0 == 256) ? Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"]"), global::System.Text.Encoding.UTF8.GetBytes(item0.Key)) : Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"]"), buffer0.Slice(0, bytesWritten0));
#else
                            bool patchContains0 = Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"]"), global::System.Text.Encoding.UTF8.GetBytes(item0.Key));
#endif
                            if (!patchContains0)
                            {
                                writer.WritePropertyName(item0.Key);
                                if ((item0.Value == null))
                                {
                                    writer.WriteNullValue();
                                    continue;
                                }
                                writer.WriteStartObject();
#if NET8_0_OR_GREATER
                                global::System.Span<byte> buffer1 = stackalloc byte[256];
#endif
                                foreach (var item1 in item0.Value)
                                {
#if NET8_0_OR_GREATER
                                    int bytesWritten1 = global::System.Text.Encoding.UTF8.GetBytes(item1.Key.AsSpan(), buffer1);
                                    bool patchContains1 = (bytesWritten1 == 256) ? Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"][\"{item0.Key}\"]"), global::System.Text.Encoding.UTF8.GetBytes(item1.Key)) : Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"][\"{item0.Key}\"]"), buffer1.Slice(0, bytesWritten1));
#else
                                    bool patchContains1 = Patch.Contains(global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"][\"{item0.Key}\"]"), global::System.Text.Encoding.UTF8.GetBytes(item1.Key));
#endif
                                    if (!patchContains1)
                                    {
                                        writer.WritePropertyName(item1.Key);
                                        writer.WriteObjectValue<global::Sample.Models.DynamicCat>(item1.Value, options);
                                    }
                                }

                                Patch.WriteTo(writer, global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"][\"{item0.Key}\"]"));
                                writer.WriteEndObject();
                            }
                        }

                        Patch.WriteTo(writer, global::System.Text.Encoding.UTF8.GetBytes($"$.propertyWithNestedDictionary[\"{item.Key}\"]"));
                        writer.WriteEndObject();
                    }
                }

                Patch.WriteTo(writer, "$.propertyWithNestedDictionary"u8);
                writer.WriteEndObject();
            }

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