// 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
#if canImport(FoundationNetworking)
  import FoundationNetworking
#endif
import GoogleCloudWkt
import GoogleLongRunning
import GoogleRpc
import GoogleCloudGax

/// Cloud Run Execution Control Plane API.
///
/// @Snippet(path: "ExecutionsQuickstart")
public class ExecutionsClient: Clients.ExecutionsProtocol {
  let inner: any Clients.ExecutionsStub
  let pollingErrorPolicy: GoogleCloudGax.PollingErrorPolicy
  let pollingBackoffPolicy: GoogleCloudGax.BackoffPolicy

  /// Creates a new `ExecutionsClient` instance.
  public init(_ options: GoogleCloudGax.ClientOptions = .init()) throws {
    var inner: any Clients.ExecutionsStub = try Clients.ExecutionsTransport(options)
    inner = Clients.ExecutionsRetry(inner, options: options)
    if let logger = options.logger {
      inner = Clients.ExecutionsLogging(inner, logger: logger)
    }
    self.inner = inner
    self.pollingErrorPolicy = options.pollingErrorPolicy
    self.pollingBackoffPolicy = options.pollingBackoffPolicy
  }

  /// Gets information about an Execution.
  ///
  /// @Snippet(path: "Executions_GetExecution")
  public func getExecution(
    request: GetExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudRunV2.Execution {
    try await self.inner.getExecution(request: request, options: options)
  }

  /// Lists Executions from a Job. Results are sorted by creation time,
  /// descending.
  ///
  /// @Snippet(path: "Executions_ListExecutions")
  public func listExecutions(
    request: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudRunV2.ListExecutionsResponse {
    try await self.inner.listExecutions(request: request, options: options)
  }

  /// Lists Executions from a Job. Results are sorted by creation time,
  /// descending.
  ///
  /// @Snippet(path: "Executions_ListExecutions")
  public func listExecutions(
    byItem: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<Execution, Swift.Error> {
    let listRpc = { (token: Swift.String) async throws -> GoogleCloudRunV2.ListExecutionsResponse in
      var request = byItem
      request.pageToken = token
      return try await self.listExecutions(request: request, options: options)
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  /// Deletes an Execution.
  ///
  /// @Snippet(path: "Executions_DeleteExecution")
  public func deleteExecution(
    request: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.deleteExecution(request: request, options: options)
  }

  /// Deletes an Execution.
  ///
  /// @Snippet(path: "Executions_DeleteExecution")
  public func deleteExecution(
    withPolling: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      guard op.done else {
        return .init(done: false, result: nil)
      }

      switch op.result {
      case .response(let anyValue):
        guard let anyValueUnwrapped = anyValue else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding(
                "Operation completed but response value was missing")))
        }
        let response = try Execution(fromAny: anyValueUnwrapped)
        return .init(done: true, result: .success(response))
      case .error(let status):
        guard let statusUnwrapped = status else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding("Operation completed but error value was missing")
            ))
        }
        let error = GoogleCloudGax.RequestError.service(
          GoogleCloudGax.ServiceError(
            code: GoogleRpc.Code(intValue: Int(statusUnwrapped.code)),
            message: statusUnwrapped.message))
        return .init(done: true, result: .failure(error))
      case .none:
        return .init(
          done: true,
          result: .failure(
            GoogleCloudGax.RequestError.binding("Operation completed but result was missing")))
      }
    }
    let rawOp = try await self.deleteExecution(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      let op = try await self.getOperation(
        request: .init().with { $0.name = rawOp.name }, options: options)
      return try extractStatus(op)
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: initialState,
      polling: options.pollingErrorPolicy ?? self.pollingErrorPolicy,
      backoff: options.pollingBackoffPolicy ?? self.pollingBackoffPolicy,
      poll: poll,
    )
  }

  /// Cancels an Execution.
  ///
  /// @Snippet(path: "Executions_CancelExecution")
  public func cancelExecution(
    request: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.cancelExecution(request: request, options: options)
  }

  /// Cancels an Execution.
  ///
  /// @Snippet(path: "Executions_CancelExecution")
  public func cancelExecution(
    withPolling: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      guard op.done else {
        return .init(done: false, result: nil)
      }

      switch op.result {
      case .response(let anyValue):
        guard let anyValueUnwrapped = anyValue else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding(
                "Operation completed but response value was missing")))
        }
        let response = try Execution(fromAny: anyValueUnwrapped)
        return .init(done: true, result: .success(response))
      case .error(let status):
        guard let statusUnwrapped = status else {
          return .init(
            done: true,
            result: .failure(
              GoogleCloudGax.RequestError.binding("Operation completed but error value was missing")
            ))
        }
        let error = GoogleCloudGax.RequestError.service(
          GoogleCloudGax.ServiceError(
            code: GoogleRpc.Code(intValue: Int(statusUnwrapped.code)),
            message: statusUnwrapped.message))
        return .init(done: true, result: .failure(error))
      case .none:
        return .init(
          done: true,
          result: .failure(
            GoogleCloudGax.RequestError.binding("Operation completed but result was missing")))
      }
    }
    let rawOp = try await self.cancelExecution(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      let op = try await self.getOperation(
        request: .init().with { $0.name = rawOp.name }, options: options)
      return try extractStatus(op)
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: initialState,
      polling: options.pollingErrorPolicy ?? self.pollingErrorPolicy,
      backoff: options.pollingBackoffPolicy ?? self.pollingBackoffPolicy,
      poll: poll,
    )
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "Executions_ListOperations")
  public func listOperations(
    request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.ListOperationsResponse {
    try await self.inner.listOperations(request: request, options: options)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "Executions_ListOperations")
  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleLongRunning.ListOperationsResponse in
      var request = byItem
      request.pageToken = token
      return try await self.listOperations(request: request, options: options)
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "Executions_GetOperation")
  func getOperation(
    request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.getOperation(request: request, options: options)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "Executions_DeleteOperation")
  public func deleteOperation(
    request: GoogleLongRunning.DeleteOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws {
    try await self.inner.deleteOperation(request: request, options: options)
  }

  /// Provides the [Operations][google.longrunning.Operations] service functionality in this service.
  ///
  /// [google.longrunning.Operations]: https://www.google.com/search?q=Swift+google.longrunning+Operations
  ///
  /// @Snippet(path: "Executions_WaitOperation")
  public func waitOperation(
    request: GoogleLongRunning.WaitOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.waitOperation(request: request, options: options)
  }
}

extension Clients {
  /// A Swift protocol to mock `ExecutionsClient`.
  ///
  /// To mock `ExecutionsClient` change your functions to receive
  /// `some ExecutionsProtocol` or `any ExecutionsProtocol`
  /// and pass a mock implementation in your tests.
  public protocol ExecutionsProtocol {
    /// See `ExecutionsClient.getExecution`.
    func getExecution(request: GetExecutionRequest) async throws -> GoogleCloudRunV2.Execution

    /// See `ExecutionsClient.getExecution`.
    func getExecution(
      name: Swift.String,
    ) async throws -> GoogleCloudRunV2.Execution

    /// See `ExecutionsClient.listExecutions`.
    func listExecutions(request: ListExecutionsRequest) async throws
      -> GoogleCloudRunV2.ListExecutionsResponse

    /// See `ExecutionsClient.listExecutions`.
    func listExecutions(
      byItem: ListExecutionsRequest
    ) throws -> any AsyncSequence<Execution, Swift.Error>

    /// See `ExecutionsClient.listExecutions`.
    func listExecutions(
      parent: Swift.String,
    ) throws -> any AsyncSequence<Execution, Swift.Error>

    /// See `ExecutionsClient.deleteExecution`.
    func deleteExecution(request: DeleteExecutionRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ExecutionsClient.deleteExecution`.
    func deleteExecution(withPolling: DeleteExecutionRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Execution>

    /// See `ExecutionsClient.deleteExecution`.
    func deleteExecution(
      name: Swift.String,
    ) async throws -> any GoogleCloudGax.PollableOperation<Execution>

    /// See `ExecutionsClient.cancelExecution`.
    func cancelExecution(request: CancelExecutionRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ExecutionsClient.cancelExecution`.
    func cancelExecution(withPolling: CancelExecutionRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Execution>

    /// See `ExecutionsClient.cancelExecution`.
    func cancelExecution(
      name: Swift.String,
    ) async throws -> any GoogleCloudGax.PollableOperation<Execution>

    /// See `ExecutionsClient.listOperations`.
    func listOperations(request: GoogleLongRunning.ListOperationsRequest) async throws
      -> GoogleLongRunning.ListOperationsResponse

    /// See `ExecutionsClient.listOperations`.
    func listOperations(
      byItem: GoogleLongRunning.ListOperationsRequest
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>

    /// See `ExecutionsClient.listOperations`.
    func listOperations(
      name: Swift.String,
      filter: Swift.String,
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>

    /// See `ExecutionsClient.deleteOperation`.
    func deleteOperation(request: GoogleLongRunning.DeleteOperationRequest) async throws

    /// See `ExecutionsClient.deleteOperation`.
    func deleteOperation(
      name: Swift.String,
    ) async throws

    /// See `ExecutionsClient.waitOperation`.
    func waitOperation(request: GoogleLongRunning.WaitOperationRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ExecutionsClient.getExecution`.
    func getExecution(
      request: GetExecutionRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudRunV2.Execution

    /// See `ExecutionsClient.listExecutions`.
    func listExecutions(
      request: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudRunV2.ListExecutionsResponse

    /// See `ExecutionsClient.listExecutions`.
    func listExecutions(
      byItem: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
    ) throws -> any AsyncSequence<Execution, Swift.Error>

    /// See `ExecutionsClient.deleteExecution`.
    func deleteExecution(
      request: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `ExecutionsClient.deleteExecution`.
    func deleteExecution(
      withPolling: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Execution>

    /// See `ExecutionsClient.cancelExecution`.
    func cancelExecution(
      request: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `ExecutionsClient.cancelExecution`.
    func cancelExecution(
      withPolling: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Execution>

    /// See `ExecutionsClient.listOperations`.
    func listOperations(
      request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.ListOperationsResponse

    /// See `ExecutionsClient.listOperations`.
    func listOperations(
      byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
    ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error>

    /// See `ExecutionsClient.deleteOperation`.
    func deleteOperation(
      request: GoogleLongRunning.DeleteOperationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws

    /// See `ExecutionsClient.waitOperation`.
    func waitOperation(
      request: GoogleLongRunning.WaitOperationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation
  }
}

// Default implementations
extension Clients.ExecutionsProtocol {
  public func getExecution(request: GetExecutionRequest) async throws -> GoogleCloudRunV2.Execution
  {
    try await self.getExecution(request: request, options: .init())
  }

  public func getExecution(
    request: GetExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudRunV2.Execution {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func getExecution(
    name: Swift.String,
  ) async throws -> GoogleCloudRunV2.Execution {
    let request = GetExecutionRequest().with {
      $0.name = name
    }
    return try await self.getExecution(request: request)
  }

  public func listExecutions(request: ListExecutionsRequest) async throws
    -> GoogleCloudRunV2.ListExecutionsResponse
  {
    try await self.listExecutions(request: request, options: .init())
  }

  public func listExecutions(
    request: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleCloudRunV2.ListExecutionsResponse {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func listExecutions(
    byItem: ListExecutionsRequest
  ) throws -> any AsyncSequence<Execution, Swift.Error> {
    try self.listExecutions(byItem: byItem, options: .init())
  }

  public func listExecutions(
    byItem: ListExecutionsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<Execution, Swift.Error> {
    let listRpc = { (token: Swift.String) async throws -> GoogleCloudRunV2.ListExecutionsResponse in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  public func listExecutions(
    parent: Swift.String,
  ) throws -> any AsyncSequence<Execution, Swift.Error> {
    let request = ListExecutionsRequest().with {
      $0.parent = parent
    }
    return try self.listExecutions(byItem: request)
  }

  public func deleteExecution(request: DeleteExecutionRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.deleteExecution(request: request, options: .init())
  }

  public func deleteExecution(
    request: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func deleteExecution(withPolling: DeleteExecutionRequest) async throws
    -> any GoogleCloudGax.PollableOperation<Execution>
  {
    try await self.deleteExecution(withPolling: withPolling, options: .init())
  }

  public func deleteExecution(
    withPolling: DeleteExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: .init(done: false, result: nil), poll: poll)
  }

  public func deleteExecution(
    name: Swift.String,
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let request = DeleteExecutionRequest().with {
      $0.name = name
    }
    return try await self.deleteExecution(withPolling: request)
  }

  public func cancelExecution(request: CancelExecutionRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.cancelExecution(request: request, options: .init())
  }

  public func cancelExecution(
    request: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func cancelExecution(withPolling: CancelExecutionRequest) async throws
    -> any GoogleCloudGax.PollableOperation<Execution>
  {
    try await self.cancelExecution(withPolling: withPolling, options: .init())
  }

  public func cancelExecution(
    withPolling: CancelExecutionRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Execution>.State in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax._PollableOperationImpl(
      initialState: .init(done: false, result: nil), poll: poll)
  }

  public func cancelExecution(
    name: Swift.String,
  ) async throws -> any GoogleCloudGax.PollableOperation<Execution> {
    let request = CancelExecutionRequest().with {
      $0.name = name
    }
    return try await self.cancelExecution(withPolling: request)
  }

  public func listOperations(request: GoogleLongRunning.ListOperationsRequest) async throws
    -> GoogleLongRunning.ListOperationsResponse
  {
    try await self.listOperations(request: request, options: .init())
  }

  public func listOperations(
    request: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.ListOperationsResponse {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    try self.listOperations(byItem: byItem, options: .init())
  }

  public func listOperations(
    byItem: GoogleLongRunning.ListOperationsRequest, options: GoogleCloudGax.RequestOptions
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let listRpc = {
      (token: Swift.String) async throws -> GoogleLongRunning.ListOperationsResponse in
      throw GoogleCloudGax.RequestError.unimplemented
    }
    return GoogleCloudGax.PaginatedResponseSequence(listRpc: listRpc)
  }

  public func listOperations(
    name: Swift.String,
    filter: Swift.String,
  ) throws -> any AsyncSequence<GoogleLongRunning.Operation, Swift.Error> {
    let request = GoogleLongRunning.ListOperationsRequest().with {
      $0.name = name
      $0.filter = filter
    }
    return try self.listOperations(byItem: request)
  }

  public func getOperation(request: GoogleLongRunning.GetOperationRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.getOperation(request: request, options: .init())
  }

  public func getOperation(
    request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func getOperation(
    name: Swift.String,
  ) async throws -> GoogleLongRunning.Operation {
    let request = GoogleLongRunning.GetOperationRequest().with {
      $0.name = name
    }
    return try await self.getOperation(request: request)
  }

  public func deleteOperation(request: GoogleLongRunning.DeleteOperationRequest) async throws {
    try await self.deleteOperation(request: request, options: .init())
  }

  public func deleteOperation(
    request: GoogleLongRunning.DeleteOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func deleteOperation(
    name: Swift.String,
  ) async throws {
    let request = GoogleLongRunning.DeleteOperationRequest().with {
      $0.name = name
    }
    try await self.deleteOperation(request: request)
  }

  public func waitOperation(request: GoogleLongRunning.WaitOperationRequest) async throws
    -> GoogleLongRunning.Operation
  {
    try await self.waitOperation(request: request, options: .init())
  }

  public func waitOperation(
    request: GoogleLongRunning.WaitOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    throw GoogleCloudGax.RequestError.unimplemented
  }
}
