// 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 Playbooks
  import Foundation
  import GoogleCloudWkt

  /// Handler can be used to define custom logic to be executed based on the
  /// user-specified triggers.
  public struct Handler: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Specifies the type of handler to invoke.
    public var handler: OneOf_Handler? = nil

    /// Initialize a new instance of `Handler`.
    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 = Handler().with { $0.eventHandler = ... }
    /// ```
    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 eventHandler = "eventHandler"
      case lifecycleHandler = "lifecycleHandler"
    }

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

      var handler: OneOf_Handler? = nil
      let handlerCheckAndSet = {
        if handler != nil {
          throw DecodingError.dataCorrupted(
            DecodingError.Context(
              codingPath: decoder.codingPath,
              debugDescription: "Multiple values set for oneof 'handler'"))
        }
        handler = $0
      }
      if let eventHandler = try container.decodeIfPresent(
        Handler.EventHandler?.self, forKey: .eventHandler)
      {
        try handlerCheckAndSet(.eventHandler(eventHandler))
      }
      if let lifecycleHandler = try container.decodeIfPresent(
        Handler.LifecycleHandler?.self, forKey: .lifecycleHandler)
      {
        try handlerCheckAndSet(.lifecycleHandler(lifecycleHandler))
      }
      self.handler = handler
    }

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

      if let choice = self.handler {
        switch choice {
        case .eventHandler(let value):
          try container.encode(value, forKey: .eventHandler)
        case .lifecycleHandler(let value):
          try container.encode(value, forKey: .lifecycleHandler)
        }
      }
    }

    /// A handler that is triggered by the specified
    /// [event][google.cloud.dialogflow.cx.v3.Handler.EventHandler.event].
    ///
    /// [google.cloud.dialogflow.cx.v3.Handler.EventHandler.event]: <doc:Handler/EventHandler/event>
    public struct EventHandler: Codable, Equatable, GoogleCloudWkt._AnyPackable,
      Sendable
    {
      /// Required. The name of the event that triggers this handler.
      public var event: Swift.String = Swift.String()

      /// Optional. The condition that must be satisfied to trigger this handler.
      public var condition: Swift.String = Swift.String()

      /// Required. The fulfillment to call when the event occurs.
      public var fulfillment: Fulfillment? = nil

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

      public static var _anyTypeUrl: Swift.String {
        return "type.googleapis.com/google.cloud.dialogflow.cx.v3.Handler.EventHandler"
      }
      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)
      }
    }

    /// A handler that is triggered on the specific
    /// [lifecycle_stage][google.cloud.dialogflow.cx.v3.Handler.LifecycleHandler.lifecycle_stage]
    /// of the playbook execution.
    ///
    /// [google.cloud.dialogflow.cx.v3.Handler.LifecycleHandler.lifecycle_stage]: <doc:Handler/LifecycleHandler/lifecycleStage>
    public struct LifecycleHandler: Codable, Equatable, GoogleCloudWkt._AnyPackable,
      Sendable
    {
      /// Required. The name of the lifecycle stage that triggers this handler.
      /// Supported values:
      /// * `playbook-start`
      /// * `pre-action-selection`
      /// * `pre-action-execution`
      public var lifecycleStage: Swift.String = Swift.String()

      /// Optional. The condition that must be satisfied to trigger this handler.
      public var condition: Swift.String = Swift.String()

      /// Required. The fulfillment to call when this handler is triggered.
      public var fulfillment: Fulfillment? = nil

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

      public static var _anyTypeUrl: Swift.String {
        return "type.googleapis.com/google.cloud.dialogflow.cx.v3.Handler.LifecycleHandler"
      }
      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)
      }
    }

    /// Specifies the type of handler to invoke.
    public enum OneOf_Handler: Codable, Equatable, Sendable {
      /// A handler triggered by event.
      indirect case eventHandler(Handler.EventHandler?)
      /// A handler triggered during specific lifecycle of the playbook execution.
      indirect case lifecycleHandler(Handler.LifecycleHandler?)
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.dialogflow.cx.v3.Handler"
    }
    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
