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

  /// Service for the `instanceSettings` resource.
  ///
  /// @Snippet(path: "instanceSettingsQuickstart")
  public class InstanceSettingsClient: Clients.InstanceSettingsProtocol {
    let inner: any Clients.InstanceSettingsStub
    let pollingErrorPolicy: GoogleCloudGax.PollingErrorPolicy
    let pollingBackoffPolicy: GoogleCloudGax.BackoffPolicy

    /// Creates a new `InstanceSettingsClient` instance.
    public init(_ options: GoogleCloudGax.ClientOptions = .init()) throws {
      var inner: any Clients.InstanceSettingsStub = try Clients.InstanceSettingsTransport(options)
      inner = Clients.InstanceSettingsRetry(inner, options: options)
      if let logger = options.logger {
        inner = Clients.InstanceSettingsLogging(inner, logger: logger)
      }
      self.inner = inner
      self.pollingErrorPolicy = options.pollingErrorPolicy
      self.pollingBackoffPolicy = options.pollingBackoffPolicy
    }

    /// Get Instance settings.
    ///
    /// @Snippet(path: "instanceSettings_get")
    public func `get`(
      request: InstanceSettingsClient.GetRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.InstanceSettings {
      try await self.inner.`get`(request: request, options: options)
    }

    /// Patch Instance settings
    ///
    /// @Snippet(path: "instanceSettings_patch")
    public func patch(
      request: InstanceSettingsClient.PatchRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.Operation {
      try await self.inner.patch(request: request, options: options)
    }

    /// Patch Instance settings
    ///
    /// @Snippet(path: "instanceSettings_patch")
    public func patch(
      withPolling: InstanceSettingsClient.PatchRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<GoogleCloudComputeV1.Operation> {
      let extractStatus = {
        (op: GoogleCloudComputeV1.Operation) throws
          -> GoogleCloudGax._PollableOperationImpl<GoogleCloudComputeV1.Operation>.State in
        guard op._done() else {
          return .init(done: false, result: nil)
        }

        do {
          try op._detectErrors()
        } catch let e as GoogleCloudGax.RequestError {
          return .init(done: true, result: .failure(e))
        }
        return .init(done: true, result: .success(op))
      }
      let rawOp = try await self.patch(request: withPolling, options: options)
      let initialState = try extractStatus(rawOp)
      let poll = {
        () async throws
          -> GoogleCloudGax._PollableOperationImpl<GoogleCloudComputeV1.Operation>.State in
        let op = try await self.getOperation(
          request: .init().with {
            $0.operation = rawOp._name()
            $0.project = withPolling.project
            $0.zone = withPolling.zone
          }, options: options)
        return try extractStatus(op)
      }
      return GoogleCloudGax._PollableOperationImpl(
        initialState: initialState,
        polling: options.pollingErrorPolicy ?? self.pollingErrorPolicy,
        backoff: options.pollingBackoffPolicy ?? self.pollingBackoffPolicy,
        poll: poll
      )
    }

    /// Retrieves the specified zone-specific Operations resource.
    ///
    /// @Snippet(path: "instanceSettings_getOperation")
    func getOperation(
      request: ZoneOperationsClient.GetRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.Operation {
      try await self.inner.getOperation(request: request, options: options)
    }
  }

  extension Clients {
    /// A Swift protocol to mock `InstanceSettingsClient`.
    ///
    /// To mock `InstanceSettingsClient` change your functions to receive
    /// `some InstanceSettingsProtocol` or `any InstanceSettingsProtocol`
    /// and pass a mock implementation in your tests.
    public protocol InstanceSettingsProtocol {
      /// See `InstanceSettingsClient.`get``.
      func `get`(request: InstanceSettingsClient.GetRequest) async throws
        -> GoogleCloudComputeV1.InstanceSettings

      /// See `InstanceSettingsClient.`get``.
      func `get`(
        project: Swift.String,
        zone: Swift.String,
      ) async throws -> GoogleCloudComputeV1.InstanceSettings

      /// See `InstanceSettingsClient.patch`.
      func patch(request: InstanceSettingsClient.PatchRequest) async throws
        -> GoogleCloudComputeV1.Operation

      /// See `InstanceSettingsClient.`get``.
      func `get`(
        request: InstanceSettingsClient.GetRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleCloudComputeV1.InstanceSettings

      /// See `InstanceSettingsClient.patch`.
      func patch(
        request: InstanceSettingsClient.PatchRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleCloudComputeV1.Operation
    }
  }

  // Default implementations
  extension Clients.InstanceSettingsProtocol {
    public func `get`(request: InstanceSettingsClient.GetRequest) async throws
      -> GoogleCloudComputeV1.InstanceSettings
    {
      try await self.`get`(request: request, options: .init())
    }

    public func `get`(
      request: InstanceSettingsClient.GetRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.InstanceSettings {
      throw GoogleCloudGax.RequestError.unimplemented
    }

    public func `get`(
      project: Swift.String,
      zone: Swift.String,
    ) async throws -> GoogleCloudComputeV1.InstanceSettings {
      let request = InstanceSettingsClient.GetRequest().with {
        $0.project = project
        $0.zone = zone
      }
      return try await self.`get`(request: request)
    }

    public func patch(request: InstanceSettingsClient.PatchRequest) async throws
      -> GoogleCloudComputeV1.Operation
    {
      try await self.patch(request: request, options: .init())
    }

    public func patch(
      request: InstanceSettingsClient.PatchRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.Operation {
      throw GoogleCloudGax.RequestError.unimplemented
    }

    public func patch(
      withPolling: InstanceSettingsClient.PatchRequest
    ) async throws -> any GoogleCloudGax.PollableOperation<GoogleCloudComputeV1.Operation> {
      try await self.patch(withPolling: withPolling, options: .init())
    }

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

    public func patch(
      project: Swift.String,
      zone: Swift.String,
      body: InstanceSettings?,
    ) async throws -> any GoogleCloudGax.PollableOperation<GoogleCloudComputeV1.Operation> {
      let request = InstanceSettingsClient.PatchRequest().with {
        $0.project = project
        $0.zone = zone
        $0.body = body
      }
      return try await self.patch(withPolling: request)
    }

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

    public func getOperation(
      request: ZoneOperationsClient.GetRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudComputeV1.Operation {
      throw GoogleCloudGax.RequestError.unimplemented
    }
  }
#endif
