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

/// Manages App Engine applications.
///
/// @Snippet(path: "ApplicationsQuickstart")
public class ApplicationsClient: Clients.ApplicationsProtocol {
  let inner: any Clients.ApplicationsStub
  let pollingErrorPolicy: GoogleCloudGax.PollingErrorPolicy
  let pollingBackoffPolicy: GoogleCloudGax.BackoffPolicy

  /// Creates a new `ApplicationsClient` instance.
  public init(_ options: GoogleCloudGax.ClientOptions = .init()) throws {
    var inner: any Clients.ApplicationsStub = try Clients.ApplicationsTransport(options)
    inner = Clients.ApplicationsRetry(inner, options: options)
    if let logger = options.logger {
      inner = Clients.ApplicationsLogging(inner, logger: logger)
    }
    self.inner = inner
    self.pollingErrorPolicy = options.pollingErrorPolicy
    self.pollingBackoffPolicy = options.pollingBackoffPolicy
  }

  /// Gets information about an application.
  ///
  /// @Snippet(path: "Applications_GetApplication")
  public func getApplication(
    request: GetApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleAppEngineV1.Application {
    try await self.inner.getApplication(request: request, options: options)
  }

  /// Creates an App Engine application for a Google Cloud Platform project.
  /// Required fields:
  ///
  /// * `id` - The ID of the target Cloud Platform project.
  /// * *location* - The [region](https://cloud.google.com/appengine/docs/locations) where you want the App Engine application located.
  ///
  /// For more information about App Engine applications, see [Managing Projects, Applications, and Billing](https://cloud.google.com/appengine/docs/standard/python/console/).
  ///
  /// @Snippet(path: "Applications_CreateApplication")
  public func createApplication(
    request: CreateApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.createApplication(request: request, options: options)
  }

  /// Creates an App Engine application for a Google Cloud Platform project.
  /// Required fields:
  ///
  /// * `id` - The ID of the target Cloud Platform project.
  /// * *location* - The [region](https://cloud.google.com/appengine/docs/locations) where you want the App Engine application located.
  ///
  /// For more information about App Engine applications, see [Managing Projects, Applications, and Billing](https://cloud.google.com/appengine/docs/standard/python/console/).
  ///
  /// @Snippet(path: "Applications_CreateApplication")
  public func createApplication(
    withPolling: CreateApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Application> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Application>.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 Application(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.createApplication(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Application>.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,
    )
  }

  /// Updates the specified Application resource.
  /// You can update the following fields:
  ///
  /// * `auth_domain` - Google authentication domain for controlling user access to the application.
  /// * `default_cookie_expiration` - Cookie expiration policy for the application.
  /// * `iap` - Identity-Aware Proxy properties for the application.
  ///
  /// @Snippet(path: "Applications_UpdateApplication")
  public func updateApplication(
    request: UpdateApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.updateApplication(request: request, options: options)
  }

  /// Updates the specified Application resource.
  /// You can update the following fields:
  ///
  /// * `auth_domain` - Google authentication domain for controlling user access to the application.
  /// * `default_cookie_expiration` - Cookie expiration policy for the application.
  /// * `iap` - Identity-Aware Proxy properties for the application.
  ///
  /// @Snippet(path: "Applications_UpdateApplication")
  public func updateApplication(
    withPolling: UpdateApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Application> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Application>.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 Application(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.updateApplication(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Application>.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,
    )
  }

  /// Recreates the required App Engine features for the specified App Engine
  /// application, for example a Cloud Storage bucket or App Engine service
  /// account.
  /// Use this method if you receive an error message about a missing feature,
  /// for example, *Error retrieving the App Engine service account*.
  /// If you have deleted your App Engine service account, this will
  /// not be able to recreate it. Instead, you should attempt to use the
  /// IAM undelete API if possible at https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/undelete?apix_params=%7B"name"%3A"projects%2F-%2FserviceAccounts%2Funique_id"%2C"resource"%3A%7B%7D%7D .
  /// If the deletion was recent, the numeric ID can be found in the Cloud
  /// Console Activity Log.
  ///
  /// @Snippet(path: "Applications_RepairApplication")
  public func repairApplication(
    request: RepairApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.repairApplication(request: request, options: options)
  }

  /// Recreates the required App Engine features for the specified App Engine
  /// application, for example a Cloud Storage bucket or App Engine service
  /// account.
  /// Use this method if you receive an error message about a missing feature,
  /// for example, *Error retrieving the App Engine service account*.
  /// If you have deleted your App Engine service account, this will
  /// not be able to recreate it. Instead, you should attempt to use the
  /// IAM undelete API if possible at https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/undelete?apix_params=%7B"name"%3A"projects%2F-%2FserviceAccounts%2Funique_id"%2C"resource"%3A%7B%7D%7D .
  /// If the deletion was recent, the numeric ID can be found in the Cloud
  /// Console Activity Log.
  ///
  /// @Snippet(path: "Applications_RepairApplication")
  public func repairApplication(
    withPolling: RepairApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> any GoogleCloudGax.PollableOperation<Application> {
    let extractStatus = {
      (op: GoogleLongRunning.Operation) throws
        -> GoogleCloudGax._PollableOperationImpl<Application>.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 Application(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.repairApplication(request: withPolling, options: options)
    let initialState = try extractStatus(rawOp)
    let poll = { () async throws -> GoogleCloudGax._PollableOperationImpl<Application>.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: "Applications_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: "Applications_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: "Applications_GetOperation")
  func getOperation(
    request: GoogleLongRunning.GetOperationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleLongRunning.Operation {
    try await self.inner.getOperation(request: request, options: options)
  }
}

extension Clients {
  /// A Swift protocol to mock `ApplicationsClient`.
  ///
  /// To mock `ApplicationsClient` change your functions to receive
  /// `some ApplicationsProtocol` or `any ApplicationsProtocol`
  /// and pass a mock implementation in your tests.
  public protocol ApplicationsProtocol {
    /// See `ApplicationsClient.getApplication`.
    func getApplication(request: GetApplicationRequest) async throws
      -> GoogleAppEngineV1.Application

    /// See `ApplicationsClient.getApplication`.
    func getApplication(
      name: Swift.String,
    ) async throws -> GoogleAppEngineV1.Application

    /// See `ApplicationsClient.createApplication`.
    func createApplication(request: CreateApplicationRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.createApplication`.
    func createApplication(withPolling: CreateApplicationRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Application>

    /// See `ApplicationsClient.updateApplication`.
    func updateApplication(request: UpdateApplicationRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.updateApplication`.
    func updateApplication(withPolling: UpdateApplicationRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Application>

    /// See `ApplicationsClient.repairApplication`.
    func repairApplication(request: RepairApplicationRequest) async throws
      -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.repairApplication`.
    func repairApplication(withPolling: RepairApplicationRequest) async throws -> any GoogleCloudGax
      .PollableOperation<Application>

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

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

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

    /// See `ApplicationsClient.getApplication`.
    func getApplication(
      request: GetApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleAppEngineV1.Application

    /// See `ApplicationsClient.createApplication`.
    func createApplication(
      request: CreateApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.createApplication`.
    func createApplication(
      withPolling: CreateApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Application>

    /// See `ApplicationsClient.updateApplication`.
    func updateApplication(
      request: UpdateApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.updateApplication`.
    func updateApplication(
      withPolling: UpdateApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Application>

    /// See `ApplicationsClient.repairApplication`.
    func repairApplication(
      request: RepairApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation

    /// See `ApplicationsClient.repairApplication`.
    func repairApplication(
      withPolling: RepairApplicationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<Application>

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

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

// Default implementations
extension Clients.ApplicationsProtocol {
  public func getApplication(request: GetApplicationRequest) async throws
    -> GoogleAppEngineV1.Application
  {
    try await self.getApplication(request: request, options: .init())
  }

  public func getApplication(
    request: GetApplicationRequest, options: GoogleCloudGax.RequestOptions
  ) async throws -> GoogleAppEngineV1.Application {
    throw GoogleCloudGax.RequestError.unimplemented
  }

  public func getApplication(
    name: Swift.String,
  ) async throws -> GoogleAppEngineV1.Application {
    let request = GetApplicationRequest().with {
      $0.name = name
    }
    return try await self.getApplication(request: request)
  }

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

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

  public func createApplication(withPolling: CreateApplicationRequest) async throws
    -> any GoogleCloudGax.PollableOperation<Application>
  {
    try await self.createApplication(withPolling: withPolling, options: .init())
  }

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

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

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

  public func updateApplication(withPolling: UpdateApplicationRequest) async throws
    -> any GoogleCloudGax.PollableOperation<Application>
  {
    try await self.updateApplication(withPolling: withPolling, options: .init())
  }

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

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

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

  public func repairApplication(withPolling: RepairApplicationRequest) async throws
    -> any GoogleCloudGax.PollableOperation<Application>
  {
    try await self.repairApplication(withPolling: withPolling, options: .init())
  }

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

  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)
  }
}
