// <auto-generated/>

#nullable disable

using System;
using System.ClientModel.Primitives;
using System.Text.Json;
using OpenAI;

namespace OpenAI.Responses
{
    [PersistableModelProxy(typeof(InternalUnknownItemParam))]
    internal abstract partial class InternalItemParam : IJsonModel<InternalItemParam>
    {
        internal InternalItemParam()
        {
        }

        void IJsonModel<InternalItemParam>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            writer.WriteStartObject();
            JsonModelWriteCore(writer, options);
            writer.WriteEndObject();
        }

        protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalItemParam>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalItemParam)} does not support writing '{format}' format.");
            }
            if (_additionalBinaryDataProperties?.ContainsKey("type") != true)
            {
                writer.WritePropertyName("type"u8);
                writer.WriteStringValue(Kind.ToString());
            }
            // Plugin customization: remove options.Format != "W" check
            if (_additionalBinaryDataProperties != null)
            {
                foreach (var item in _additionalBinaryDataProperties)
                {
                    if (ModelSerializationExtensions.IsSentinelValue(item.Value))
                    {
                        continue;
                    }
                    writer.WritePropertyName(item.Key);
#if NET6_0_OR_GREATER
                    writer.WriteRawValue(item.Value);
#else
                    using (JsonDocument document = JsonDocument.Parse(item.Value))
                    {
                        JsonSerializer.Serialize(writer, document.RootElement);
                    }
#endif
                }
            }
        }

        InternalItemParam IJsonModel<InternalItemParam>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options);

        protected virtual InternalItemParam JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalItemParam>)this).GetFormatFromOptions(options) : options.Format;
            if (format != "J")
            {
                throw new FormatException($"The model {nameof(InternalItemParam)} does not support reading '{format}' format.");
            }
            using JsonDocument document = JsonDocument.ParseValue(ref reader);
            return DeserializeInternalItemParam(document.RootElement, options);
        }

        internal static InternalItemParam DeserializeInternalItemParam(JsonElement element, ModelReaderWriterOptions options)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return null;
            }
            if (element.TryGetProperty("type"u8, out JsonElement discriminator))
            {
                switch (discriminator.GetString())
                {
                    case "file_search_call":
                        return InternalFileSearchToolCallItemParam.DeserializeInternalFileSearchToolCallItemParam(element, options);
                    case "computer_call":
                        return InternalComputerUsePreviewToolCallItemParam.DeserializeInternalComputerUsePreviewToolCallItemParam(element, options);
                    case "computer_call_output":
                        return InternalComputerUsePreviewToolCallOutputItemParam.DeserializeInternalComputerUsePreviewToolCallOutputItemParam(element, options);
                    case "web_search_call":
                        return InternalWebSearchToolCallItemParam.DeserializeInternalWebSearchToolCallItemParam(element, options);
                    case "function_call":
                        return InternalFunctionToolCallItemParam.DeserializeInternalFunctionToolCallItemParam(element, options);
                    case "function_call_output":
                        return InternalFunctionToolCallOutputItemParam.DeserializeInternalFunctionToolCallOutputItemParam(element, options);
                    case "reasoning":
                        return InternalReasoningItemParam.DeserializeInternalReasoningItemParam(element, options);
                    case "item_reference":
                        return InternalItemReferenceItemParam.DeserializeInternalItemReferenceItemParam(element, options);
                    case "image_generation_call":
                        return InternalImageGenToolCallItemParam.DeserializeInternalImageGenToolCallItemParam(element, options);
                    case "code_interpreter_call":
                        return InternalCodeInterpreterToolCallItemParam.DeserializeInternalCodeInterpreterToolCallItemParam(element, options);
                    case "local_shell_call":
                        return InternalLocalShellToolCallItemParam.DeserializeInternalLocalShellToolCallItemParam(element, options);
                    case "local_shell_call_output":
                        return InternalLocalShellToolCallOutputItemParam.DeserializeInternalLocalShellToolCallOutputItemParam(element, options);
                    case "mcp_list_tools":
                        return InternalMCPListToolsItemParam.DeserializeInternalMCPListToolsItemParam(element, options);
                    case "mcp_approval_request":
                        return InternalMCPApprovalRequestItemParam.DeserializeInternalMCPApprovalRequestItemParam(element, options);
                    case "mcp_approval_response":
                        return InternalMCPApprovalResponseItemParam.DeserializeInternalMCPApprovalResponseItemParam(element, options);
                    case "mcp_call":
                        return InternalMCPCallItemParam.DeserializeInternalMCPCallItemParam(element, options);
                    case "message":
                        return InternalResponsesMessageItemParam.DeserializeInternalResponsesMessageItemParam(element, options);
                }
            }
            return InternalUnknownItemParam.DeserializeInternalUnknownItemParam(element, options);
        }

        BinaryData IPersistableModel<InternalItemParam>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);

        protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalItemParam>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
                default:
                    throw new FormatException($"The model {nameof(InternalItemParam)} does not support writing '{options.Format}' format.");
            }
        }

        InternalItemParam IPersistableModel<InternalItemParam>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options);

        protected virtual InternalItemParam PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
        {
            string format = options.Format == "W" ? ((IPersistableModel<InternalItemParam>)this).GetFormatFromOptions(options) : options.Format;
            switch (format)
            {
                case "J":
                    using (JsonDocument document = JsonDocument.Parse(data))
                    {
                        return DeserializeInternalItemParam(document.RootElement, options);
                    }
                default:
                    throw new FormatException($"The model {nameof(InternalItemParam)} does not support reading '{options.Format}' format.");
            }
        }

        string IPersistableModel<InternalItemParam>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
    }
}
