// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// The top-level message returned from
/// [SessionService.BidiRunSession][google.cloud.ces.v1.SessionService.BidiRunSession]
/// method.
///
/// [google.cloud.ces.v1.SessionService.BidiRunSession]: <doc:SessionService/bidiRunSession(request:)>
public struct BidiSessionServerMessage: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// The type of the message.
  public var messageType: OneOf_MessageType? = nil

  /// Initialize a new instance of `BidiSessionServerMessage`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = BidiSessionServerMessage().with { $0.sessionOutput = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case sessionOutput = "sessionOutput"
    case recognitionResult = "recognitionResult"
    case interruptionSignal = "interruptionSignal"
    case endSession = "endSession"
    case goAway = "goAway"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    var messageType: OneOf_MessageType? = nil
    let messageTypeCheckAndSet = {
      if messageType != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'messageType'"))
      }
      messageType = $0
    }
    if let sessionOutput = try container.decodeIfPresent(
      SessionOutput?.self, forKey: .sessionOutput)
    {
      try messageTypeCheckAndSet(.sessionOutput(sessionOutput))
    }
    if let recognitionResult = try container.decodeIfPresent(
      RecognitionResult?.self, forKey: .recognitionResult)
    {
      try messageTypeCheckAndSet(.recognitionResult(recognitionResult))
    }
    if let interruptionSignal = try container.decodeIfPresent(
      InterruptionSignal?.self, forKey: .interruptionSignal)
    {
      try messageTypeCheckAndSet(.interruptionSignal(interruptionSignal))
    }
    if let endSession = try container.decodeIfPresent(EndSession?.self, forKey: .endSession) {
      try messageTypeCheckAndSet(.endSession(endSession))
    }
    if let goAway = try container.decodeIfPresent(GoAway?.self, forKey: .goAway) {
      try messageTypeCheckAndSet(.goAway(goAway))
    }
    self.messageType = messageType
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    if let choice = self.messageType {
      switch choice {
      case .sessionOutput(let value):
        try container.encode(value, forKey: .sessionOutput)
      case .recognitionResult(let value):
        try container.encode(value, forKey: .recognitionResult)
      case .interruptionSignal(let value):
        try container.encode(value, forKey: .interruptionSignal)
      case .endSession(let value):
        try container.encode(value, forKey: .endSession)
      case .goAway(let value):
        try container.encode(value, forKey: .goAway)
      }
    }
  }

  /// The type of the message.
  public enum OneOf_MessageType: Codable, Equatable, Sendable {
    /// Optional. Processing result from the CES agent.
    indirect case sessionOutput(SessionOutput?)
    /// Optional. Realtime speech recognition result for the audio input.
    indirect case recognitionResult(RecognitionResult?)
    /// Optional. Indicates the agent's audio response has been interrupted.
    indirect case interruptionSignal(InterruptionSignal?)
    /// Optional. Indicates that the session has ended.
    indirect case endSession(EndSession?)
    /// Optional. Indicates that the server will disconnect soon and the client
    /// should half-close and restart the connection.
    indirect case goAway(GoAway?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.ces.v1.BidiSessionServerMessage"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
