// 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.

#if BackendServices || RegionBackendServices
  import Foundation
  import GoogleCloudWkt

  /// Connection Tracking configuration for this BackendService.
  public struct BackendServiceConnectionTrackingPolicy: Codable, Equatable, GoogleCloudWkt
      ._AnyPackable,
    Sendable
  {
    /// Specifies connection persistence when backends are unhealthy. The default
    /// value is DEFAULT_FOR_PROTOCOL.
    ///
    /// If set to DEFAULT_FOR_PROTOCOL, the existing connections
    /// persist on unhealthy backends only for connection-oriented protocols
    /// (TCP and SCTP) and only if the Tracking Mode isPER_CONNECTION (default tracking mode) or the Session
    /// Affinity is configured for 5-tuple. They do not persist forUDP.
    ///
    /// If set to NEVER_PERSIST, after a backend becomes unhealthy,
    /// the existing connections on the unhealthy backend are never persisted on
    /// the unhealthy backend. They are always diverted to newly selected healthy
    /// backends (unless all backends are unhealthy).
    ///
    /// If set to ALWAYS_PERSIST, existing connections always
    /// persist on unhealthy backends regardless of protocol and session
    /// affinity. It is generally not recommended to use this mode overriding the
    /// default.
    ///
    /// For more details, see [Connection Persistence for Network Load
    /// Balancing](https://cloud.google.com/load-balancing/docs/network/networklb-backend-service#connection-persistence)
    /// and [Connection Persistence for Internal TCP/UDP Load
    /// Balancing](https://cloud.google.com/load-balancing/docs/internal#connection-persistence).
    public var connectionPersistenceOnUnhealthyBackends:
      BackendServiceConnectionTrackingPolicy.ConnectionPersistenceOnUnhealthyBackends? = nil

    /// Enable Strong Session Affinity for external passthrough Network Load
    /// Balancers. This option is not available publicly.
    public var enableStrongAffinity: Swift.Bool? = nil

    /// Specifies how long to keep a Connection Tracking entry while there is no
    /// matching traffic (in seconds).
    ///
    /// For internal passthrough Network Load Balancers:
    ///
    ///    - The minimum (default) is 10 minutes and the maximum is 16 hours.
    ///    - It can be set only if Connection Tracking is less than 5-tuple
    ///    (i.e. Session Affinity is CLIENT_IP_NO_DESTINATION,CLIENT_IP or CLIENT_IP_PROTO, and Tracking
    ///    Mode is PER_SESSION).
    ///
    ///
    ///
    /// For external passthrough Network Load Balancers the default is 60
    /// seconds. This option is not available publicly.
    public var idleTimeoutSec: Swift.Int32? = nil

    /// Specifies the key used for connection tracking. There are two
    /// options:
    ///
    ///    - PER_CONNECTION: This is the default mode. The Connection
    ///    Tracking is performed as per the Connection Key (default Hash Method) for
    ///    the specific protocol.
    ///    - PER_SESSION: The Connection Tracking is performed as per
    ///    the configured Session Affinity. It matches the configured Session
    ///    Affinity.
    ///
    ///
    ///
    /// For more details, see [Tracking Mode for Network Load
    /// Balancing](https://cloud.google.com/load-balancing/docs/network/networklb-backend-service#tracking-mode)
    /// and [Tracking Mode for Internal TCP/UDP Load
    /// Balancing](https://cloud.google.com/load-balancing/docs/internal#tracking-mode).
    public var trackingMode: BackendServiceConnectionTrackingPolicy.TrackingMode? = nil

    /// Initialize a new instance of `BackendServiceConnectionTrackingPolicy`.
    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 = BackendServiceConnectionTrackingPolicy().with { $0.connectionPersistenceOnUnhealthyBackends = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    /// The enumerated type for the [connectionPersistenceOnUnhealthyBackends][google.cloud.compute.v1.BackendServiceConnectionTrackingPolicy.connectionPersistenceOnUnhealthyBackends] field.
    ///
    /// [google.cloud.compute.v1.BackendServiceConnectionTrackingPolicy.connectionPersistenceOnUnhealthyBackends]: <doc:BackendServiceConnectionTrackingPolicy/ConnectionPersistenceOnUnhealthyBackends>
    public enum ConnectionPersistenceOnUnhealthyBackends: Codable, Equatable, Sendable {
      case alwaysPersist
      case defaultForProtocol
      case neverPersist
      /// Encodes an unknown integer value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownIntValue(Int)
      /// Encodes an unknown string value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownStringValue(String)

      public init() {
        self = .alwaysPersist
      }

      /// Returns the integer value associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown string value, this returns `nil`.
      public var intValue: Int? {
        switch self {
        case .alwaysPersist: return 0
        case .defaultForProtocol: return 1
        case .neverPersist: return 2
        case .unknownIntValue(let v): return v
        case .unknownStringValue: return nil
        }
      }

      /// Returns the string value (or name) associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
      public var stringValue: Swift.String? {
        switch self {
        case .alwaysPersist: return "ALWAYS_PERSIST"
        case .defaultForProtocol: return "DEFAULT_FOR_PROTOCOL"
        case .neverPersist: return "NEVER_PERSIST"
        case .unknownIntValue: return nil
        case .unknownStringValue(let v): return v
        }
      }

      /// Initialize from a string value.
      ///
      /// If the value is unknown, this initializes to [`unknownStringValue`](doc:ConnectionPersistenceOnUnhealthyBackends/unknownStringValue(_:)).
      public init(stringValue: Swift.String) {
        switch stringValue {
        case "ALWAYS_PERSIST": self = .alwaysPersist
        case "DEFAULT_FOR_PROTOCOL": self = .defaultForProtocol
        case "NEVER_PERSIST": self = .neverPersist
        default: self = .unknownStringValue(stringValue)
        }
      }

      /// Initialize from an integer value.
      ///
      /// If the value is unknown, this initializes to [`unknownIntValue`](doc:ConnectionPersistenceOnUnhealthyBackends/unknownIntValue(_:)).
      public init(intValue: Int) {
        switch intValue {
        case 0: self = .alwaysPersist
        case 1: self = .defaultForProtocol
        case 2: self = .neverPersist
        default: self = .unknownIntValue(intValue)
        }
      }

      public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let v = try? container.decode(Int.self) {
          self.init(intValue: v)
          return
        }
        if let s = try? container.decode(String.self) {
          if let v = Int(s) {
            self.init(intValue: v)
          } else {
            self.init(stringValue: s)
          }
          return
        }
        throw DecodingError.dataCorruptedError(
          in: container, debugDescription: "Expected enum value, must be integer or string.")
      }

      public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .alwaysPersist: return try container.encode(0)
        case .defaultForProtocol: return try container.encode(1)
        case .neverPersist: return try container.encode(2)
        case .unknownIntValue(let v): return try container.encode(v)
        case .unknownStringValue(let v): return try container.encode(v)
        }
      }
    }

    /// The enumerated type for the [trackingMode][google.cloud.compute.v1.BackendServiceConnectionTrackingPolicy.trackingMode] field.
    ///
    /// [google.cloud.compute.v1.BackendServiceConnectionTrackingPolicy.trackingMode]: <doc:BackendServiceConnectionTrackingPolicy/TrackingMode>
    public enum TrackingMode: Codable, Equatable, Sendable {
      case invalidTrackingMode
      case perConnection
      case perSession
      /// Encodes an unknown integer value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownIntValue(Int)
      /// Encodes an unknown string value.
      ///
      /// The most common cause for an unknown values is for the service to send
      /// a value unknown to the library. We recommend you update your library to
      /// the latest version.
      case unknownStringValue(String)

      public init() {
        self = .invalidTrackingMode
      }

      /// Returns the integer value associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown string value, this returns `nil`.
      public var intValue: Int? {
        switch self {
        case .invalidTrackingMode: return 0
        case .perConnection: return 1
        case .perSession: return 2
        case .unknownIntValue(let v): return v
        case .unknownStringValue: return nil
        }
      }

      /// Returns the string value (or name) associated with the enumeration.
      ///
      /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
      public var stringValue: Swift.String? {
        switch self {
        case .invalidTrackingMode: return "INVALID_TRACKING_MODE"
        case .perConnection: return "PER_CONNECTION"
        case .perSession: return "PER_SESSION"
        case .unknownIntValue: return nil
        case .unknownStringValue(let v): return v
        }
      }

      /// Initialize from a string value.
      ///
      /// If the value is unknown, this initializes to [`unknownStringValue`](doc:TrackingMode/unknownStringValue(_:)).
      public init(stringValue: Swift.String) {
        switch stringValue {
        case "INVALID_TRACKING_MODE": self = .invalidTrackingMode
        case "PER_CONNECTION": self = .perConnection
        case "PER_SESSION": self = .perSession
        default: self = .unknownStringValue(stringValue)
        }
      }

      /// Initialize from an integer value.
      ///
      /// If the value is unknown, this initializes to [`unknownIntValue`](doc:TrackingMode/unknownIntValue(_:)).
      public init(intValue: Int) {
        switch intValue {
        case 0: self = .invalidTrackingMode
        case 1: self = .perConnection
        case 2: self = .perSession
        default: self = .unknownIntValue(intValue)
        }
      }

      public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let v = try? container.decode(Int.self) {
          self.init(intValue: v)
          return
        }
        if let s = try? container.decode(String.self) {
          if let v = Int(s) {
            self.init(intValue: v)
          } else {
            self.init(stringValue: s)
          }
          return
        }
        throw DecodingError.dataCorruptedError(
          in: container, debugDescription: "Expected enum value, must be integer or string.")
      }

      public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .invalidTrackingMode: return try container.encode(0)
        case .perConnection: return try container.encode(1)
        case .perSession: return try container.encode(2)
        case .unknownIntValue(let v): return try container.encode(v)
        case .unknownStringValue(let v): return try container.encode(v)
        }
      }
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.compute.v1.BackendServiceConnectionTrackingPolicy"
    }
    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)
    }
  }
#endif
