// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Xml;

namespace Sample
{
    public partial class ModelSerializationExtensions
    {
        public static void WriteObjectValue<T>(this global::System.Text.Json.Utf8JsonWriter writer, T value, global::System.ClientModel.Primitives.ModelReaderWriterOptions options = ((global::System.ClientModel.Primitives.ModelReaderWriterOptions)null))
        {
            switch (value)
            {
                case null:
                    writer.WriteNullValue();
                    break;
                case global::System.ClientModel.Primitives.IJsonModel<T> jsonModel:
                    jsonModel.Write(writer, (options ?? global::Sample.ModelSerializationExtensions.WireOptions));
                    break;
                case global::System.Byte[] bytes:
                    writer.WriteBase64StringValue(bytes);
                    break;
                case global::System.BinaryData bytes0:
                    writer.WriteBase64StringValue(bytes0);
                    break;
                case global::System.Text.Json.JsonElement json:
                    json.WriteTo(writer);
                    break;
                case int i:
                    writer.WriteNumberValue(i);
                    break;
                case decimal d:
                    writer.WriteNumberValue(d);
                    break;
                case double d0:
                    if (double.IsNaN(d0))
                    {
                        writer.WriteStringValue("NaN");
                    }
                    else
                    {
                        writer.WriteNumberValue(d0);
                    }
                    break;
                case float f:
                    writer.WriteNumberValue(f);
                    break;
                case long l:
                    writer.WriteNumberValue(l);
                    break;
                case string s:
                    writer.WriteStringValue(s);
                    break;
                case bool b:
                    writer.WriteBooleanValue(b);
                    break;
                case global::System.Guid g:
                    writer.WriteStringValue(g);
                    break;
                case global::System.DateTimeOffset dateTimeOffset:
                    writer.WriteStringValue(dateTimeOffset, "O");
                    break;
                case global::System.DateTime dateTime:
                    writer.WriteStringValue(dateTime, "O");
                    break;
                case global::System.Collections.Generic.IEnumerable<global::System.Collections.Generic.KeyValuePair<string, object>> enumerable:
                    writer.WriteStartObject();
                    foreach (var pair in enumerable)
                    {
                        writer.WritePropertyName(pair.Key);
                        writer.WriteObjectValue<object>(pair.Value, options);
                    }
                    writer.WriteEndObject();
                    break;
                case global::System.Collections.Generic.IEnumerable<object> objectEnumerable:
                    writer.WriteStartArray();
                    foreach (var item in objectEnumerable)
                    {
                        writer.WriteObjectValue<object>(item, options);
                    }
                    writer.WriteEndArray();
                    break;
                case global::System.TimeSpan timeSpan:
                    writer.WriteStringValue(timeSpan, "P");
                    break;
                default:
                    throw new global::System.NotSupportedException($"Not supported type {value.GetType()}");
            }
        }

        public static void WriteObjectValue(this global::System.Text.Json.Utf8JsonWriter writer, object value, global::System.ClientModel.Primitives.ModelReaderWriterOptions options = ((global::System.ClientModel.Primitives.ModelReaderWriterOptions)null))
        {
            writer.WriteObjectValue<object>(value, options);
        }

        public static void WriteObjectValue<T>(this global::System.Xml.XmlWriter writer, T value, global::System.ClientModel.Primitives.ModelReaderWriterOptions options = ((global::System.ClientModel.Primitives.ModelReaderWriterOptions)null), string nameHint = ((string)null))
        {
            switch (value)
            {
                case global::System.ClientModel.Primitives.IPersistableModel<T> persistableModel:
                    global::System.BinaryData data = global::System.ClientModel.Primitives.ModelReaderWriter.Write(persistableModel, (options ?? global::Sample.ModelSerializationExtensions.WireOptions), global::Sample.SampleContext.Default);
                    using (global::System.IO.Stream stream = data.ToStream())
                    {
                        using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(stream, XmlReaderSettings))
                        {
                            reader.MoveToContent();
                            if ((nameHint != null))
                            {
                                writer.WriteStartElement(nameHint);
                                writer.WriteAttributes(reader, true);
                                reader.ReadStartElement();
                                while ((reader.NodeType != global::System.Xml.XmlNodeType.EndElement))
                                {
                                    writer.WriteNode(reader, true);
                                }
                                writer.WriteEndElement();
                            }
                            else
                            {
                                writer.WriteAttributes(reader, true);
                                reader.ReadStartElement();
                                while ((reader.NodeType != global::System.Xml.XmlNodeType.EndElement))
                                {
                                    writer.WriteNode(reader, true);
                                }
                            }
                        }
                    }
                    return;
                case global::System.ClientModel.FileBinaryContent fileBinaryContent:
                    writer.WriteFileBinaryContent(fileBinaryContent);
                    return;
                default:
                    throw new global::System.NotSupportedException($"Not supported type {typeof(T)}");
            }
        }
    }
}
