// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.IO;
using System.Xml;
using Sample;

namespace Sample.Models
{
    public partial class OuterModel : global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.OuterModel>
    {
        protected virtual global::System.BinaryData PersistableModelWriteCore(global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.OuterModel>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "X":
                    using (global::System.IO.MemoryStream stream = new global::System.IO.MemoryStream(256))
                    {
                        using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(stream, global::Sample.ModelSerializationExtensions.XmlWriterSettings))
                        {
                            this.WriteXml(writer, options, "OuterModel");
                        }
                        if ((stream.Position > int.MaxValue))
                        {
                            return global::System.BinaryData.FromStream(stream);
                        }
                        else
                        {
                            return new global::System.BinaryData(stream.GetBuffer().AsMemory(0, ((int)stream.Position)));
                        }
                    }
                default:
                    throw new global::System.FormatException($"The model {nameof(global::Sample.Models.OuterModel)} does not support writing '{options.Format}' format.");
            }
        }

        global::System.BinaryData global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.OuterModel>.Write(global::System.ClientModel.Primitives.ModelReaderWriterOptions options) => this.PersistableModelWriteCore(options);

        private void WriteXml(global::System.Xml.XmlWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options, string nameHint)
        {
            if ((nameHint != null))
            {
                writer.WriteStartElement(nameHint);
            }

            this.XmlModelWriteCore(writer, options);

            if ((nameHint != null))
            {
                writer.WriteEndElement();
            }
        }

        internal virtual void XmlModelWriteCore(global::System.Xml.XmlWriter writer, global::System.ClientModel.Primitives.ModelReaderWriterOptions options)
        {
            string format = (options.Format == "W") ? ((global::System.ClientModel.Primitives.IPersistableModel<global::Sample.Models.OuterModel>)this).GetFormatFromOptions(options) : options.Format;
            if ((format != "X"))
            {
                throw new global::System.FormatException($"The model {nameof(global::Sample.Models.OuterModel)} does not support writing '{format}' format.");
            }

            if (global::Sample.Optional.IsDefined(Inner))
            {
                writer.WriteStartElement("inner");
                writer.WriteObjectValue<global::Sample.Models.InnerModel>(Inner, options);
                writer.WriteEndElement();
            }
        }
    }
}
