// <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("$.cats"u8))
            {
                if (!Patch.IsRemoved("$.cats"u8))
                {
                    writer.WritePropertyName("cats"u8);
                    writer.WriteRawValue(Patch.GetJson("$.cats"u8));
                }
            }
            else if (global::Sample.Optional.IsCollectionDefined(Cats))
            {
                writer.WritePropertyName("cats"u8);
                writer.WriteStartArray();
                for (int i = 0; (i < Cats.Count); i++)
                {
                    if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.cats[{i}]")))
                    {
                        continue;
                    }
                    writer.WriteObjectValue<global::Sample.Models.Cat>(Cats[i], options);
                }
                Patch.WriteTo(writer, "$.cats"u8);
                writer.WriteEndArray();
            }
            if (Patch.Contains("$.names"u8))
            {
                if (!Patch.IsRemoved("$.names"u8))
                {
                    writer.WritePropertyName("names"u8);
                    writer.WriteRawValue(Patch.GetJson("$.names"u8));
                }
            }
            else
            {
                writer.WritePropertyName("names"u8);
                writer.WriteStartArray();
                for (int i = 0; (i < Names.Count); i++)
                {
                    if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.names[{i}]")))
                    {
                        continue;
                    }
                    if ((Names[i] == null))
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(Names[i]);
                }
                Patch.WriteTo(writer, "$.names"u8);
                writer.WriteEndArray();
            }
            if (Patch.Contains("$.optionalNames"u8))
            {
                if (!Patch.IsRemoved("$.optionalNames"u8))
                {
                    writer.WritePropertyName("optionalNames"u8);
                    writer.WriteRawValue(Patch.GetJson("$.optionalNames"u8));
                }
            }
            else if (global::Sample.Optional.IsCollectionDefined(OptionalNames))
            {
                writer.WritePropertyName("optionalNames"u8);
                writer.WriteStartArray();
                for (int i = 0; (i < OptionalNames.Count); i++)
                {
                    if (Patch.IsRemoved(global::System.Text.Encoding.UTF8.GetBytes($"$.optionalNames[{i}]")))
                    {
                        continue;
                    }
                    if ((OptionalNames[i] == null))
                    {
                        writer.WriteNullValue();
                        continue;
                    }
                    writer.WriteStringValue(OptionalNames[i]);
                }
                Patch.WriteTo(writer, "$.optionalNames"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.
        }
    }
}
