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

  /// Service for ingesting end user actions on a website to Discovery Engine API.
  ///
  /// @Snippet(path: "UserEventServiceQuickstart")
  public class UserEventServiceClient: Clients.UserEventServiceProtocol {
    let inner: any Clients.UserEventServiceStub
    let pollingErrorPolicy: GoogleCloudGax.PollingErrorPolicy
    let pollingBackoffPolicy: GoogleCloudGax.BackoffPolicy

    /// Creates a new `UserEventServiceClient` instance.
    public init(_ options: GoogleCloudGax.ClientOptions = .init()) throws {
      var inner: any Clients.UserEventServiceStub = try Clients.UserEventServiceTransport(options)
      inner = Clients.UserEventServiceRetry(inner, options: options)
      if let logger = options.logger {
        inner = Clients.UserEventServiceLogging(inner, logger: logger)
      }
      self.inner = inner
      self.pollingErrorPolicy = options.pollingErrorPolicy
      self.pollingBackoffPolicy = options.pollingBackoffPolicy
    }

    /// Writes a single user event.
    ///
    /// @Snippet(path: "UserEventService_WriteUserEvent")
    public func writeUserEvent(
      request: WriteUserEventRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudDiscoveryEngineV1.UserEvent {
      try await self.inner.writeUserEvent(request: request, options: options)
    }

    /// Writes a single user event from the browser. This uses a GET request to
    /// due to browser restriction of POST-ing to a third-party domain.
    ///
    /// This method is used only by the Discovery Engine API JavaScript pixel and
    /// Google Tag Manager. Users should not call this method directly.
    ///
    /// @Snippet(path: "UserEventService_CollectUserEvent")
    public func collectUserEvent(
      request: CollectUserEventRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleApi.HttpBody {
      try await self.inner.collectUserEvent(request: request, options: options)
    }

    /// Deletes permanently all user events specified by the filter provided.
    /// Depending on the number of events specified by the filter, this operation
    /// could take hours or days to complete. To test a filter, use the list
    /// command first.
    ///
    /// @Snippet(path: "UserEventService_PurgeUserEvents")
    public func purgeUserEvents(
      request: PurgeUserEventsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation {
      try await self.inner.purgeUserEvents(request: request, options: options)
    }

    /// Deletes permanently all user events specified by the filter provided.
    /// Depending on the number of events specified by the filter, this operation
    /// could take hours or days to complete. To test a filter, use the list
    /// command first.
    ///
    /// @Snippet(path: "UserEventService_PurgeUserEvents")
    public func purgeUserEvents(
      withPolling: PurgeUserEventsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<PurgeUserEventsResponse> {
      let extractStatus = {
        (op: GoogleLongRunning.Operation) throws
          -> GoogleCloudGax._PollableOperationImpl<PurgeUserEventsResponse>.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 PurgeUserEventsResponse(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.purgeUserEvents(request: withPolling, options: options)
      let initialState = try extractStatus(rawOp)
      let poll = {
        () async throws -> GoogleCloudGax._PollableOperationImpl<PurgeUserEventsResponse>.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,
      )
    }

    /// Bulk import of user events. Request processing might be
    /// synchronous. Events that already exist are skipped.
    /// Use this method for backfilling historical user events.
    ///
    /// Operation.response is of type ImportResponse. Note that it is
    /// possible for a subset of the items to be successfully inserted.
    /// Operation.metadata is of type ImportMetadata.
    ///
    /// @Snippet(path: "UserEventService_ImportUserEvents")
    public func importUserEvents(
      request: ImportUserEventsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleLongRunning.Operation {
      try await self.inner.importUserEvents(request: request, options: options)
    }

    /// Bulk import of user events. Request processing might be
    /// synchronous. Events that already exist are skipped.
    /// Use this method for backfilling historical user events.
    ///
    /// Operation.response is of type ImportResponse. Note that it is
    /// possible for a subset of the items to be successfully inserted.
    /// Operation.metadata is of type ImportMetadata.
    ///
    /// @Snippet(path: "UserEventService_ImportUserEvents")
    public func importUserEvents(
      withPolling: ImportUserEventsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<ImportUserEventsResponse> {
      let extractStatus = {
        (op: GoogleLongRunning.Operation) throws
          -> GoogleCloudGax._PollableOperationImpl<ImportUserEventsResponse>.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 ImportUserEventsResponse(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.importUserEvents(request: withPolling, options: options)
      let initialState = try extractStatus(rawOp)
      let poll = {
        () async throws -> GoogleCloudGax._PollableOperationImpl<ImportUserEventsResponse>.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: "UserEventService_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: "UserEventService_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: "UserEventService_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: "UserEventService_CancelOperation")
    public func cancelOperation(
      request: GoogleLongRunning.CancelOperationRequest, options: GoogleCloudGax.RequestOptions
    ) async throws {
      try await self.inner.cancelOperation(request: request, options: options)
    }
  }

  extension Clients {
    /// A Swift protocol to mock `UserEventServiceClient`.
    ///
    /// To mock `UserEventServiceClient` change your functions to receive
    /// `some UserEventServiceProtocol` or `any UserEventServiceProtocol`
    /// and pass a mock implementation in your tests.
    public protocol UserEventServiceProtocol {
      /// See `UserEventServiceClient.writeUserEvent`.
      func writeUserEvent(request: WriteUserEventRequest) async throws
        -> GoogleCloudDiscoveryEngineV1.UserEvent

      /// See `UserEventServiceClient.collectUserEvent`.
      func collectUserEvent(request: CollectUserEventRequest) async throws -> GoogleApi.HttpBody

      /// See `UserEventServiceClient.purgeUserEvents`.
      func purgeUserEvents(request: PurgeUserEventsRequest) async throws
        -> GoogleLongRunning.Operation

      /// See `UserEventServiceClient.purgeUserEvents`.
      func purgeUserEvents(withPolling: PurgeUserEventsRequest) async throws -> any GoogleCloudGax
        .PollableOperation<PurgeUserEventsResponse>

      /// See `UserEventServiceClient.importUserEvents`.
      func importUserEvents(request: ImportUserEventsRequest) async throws
        -> GoogleLongRunning.Operation

      /// See `UserEventServiceClient.importUserEvents`.
      func importUserEvents(withPolling: ImportUserEventsRequest) async throws -> any GoogleCloudGax
        .PollableOperation<ImportUserEventsResponse>

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

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

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

      /// See `UserEventServiceClient.cancelOperation`.
      func cancelOperation(request: GoogleLongRunning.CancelOperationRequest) async throws

      /// See `UserEventServiceClient.cancelOperation`.
      func cancelOperation(
        name: Swift.String,
      ) async throws

      /// See `UserEventServiceClient.writeUserEvent`.
      func writeUserEvent(
        request: WriteUserEventRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleCloudDiscoveryEngineV1.UserEvent

      /// See `UserEventServiceClient.collectUserEvent`.
      func collectUserEvent(
        request: CollectUserEventRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleApi.HttpBody

      /// See `UserEventServiceClient.purgeUserEvents`.
      func purgeUserEvents(
        request: PurgeUserEventsRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleLongRunning.Operation

      /// See `UserEventServiceClient.purgeUserEvents`.
      func purgeUserEvents(
        withPolling: PurgeUserEventsRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> any GoogleCloudGax.PollableOperation<PurgeUserEventsResponse>

      /// See `UserEventServiceClient.importUserEvents`.
      func importUserEvents(
        request: ImportUserEventsRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> GoogleLongRunning.Operation

      /// See `UserEventServiceClient.importUserEvents`.
      func importUserEvents(
        withPolling: ImportUserEventsRequest, options: GoogleCloudGax.RequestOptions
      ) async throws -> any GoogleCloudGax.PollableOperation<ImportUserEventsResponse>

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

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

      /// See `UserEventServiceClient.cancelOperation`.
      func cancelOperation(
        request: GoogleLongRunning.CancelOperationRequest, options: GoogleCloudGax.RequestOptions
      ) async throws
    }
  }

  // Default implementations
  extension Clients.UserEventServiceProtocol {
    public func writeUserEvent(request: WriteUserEventRequest) async throws
      -> GoogleCloudDiscoveryEngineV1.UserEvent
    {
      try await self.writeUserEvent(request: request, options: .init())
    }

    public func writeUserEvent(
      request: WriteUserEventRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleCloudDiscoveryEngineV1.UserEvent {
      throw GoogleCloudGax.RequestError.unimplemented
    }

    public func collectUserEvent(request: CollectUserEventRequest) async throws
      -> GoogleApi.HttpBody
    {
      try await self.collectUserEvent(request: request, options: .init())
    }

    public func collectUserEvent(
      request: CollectUserEventRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> GoogleApi.HttpBody {
      throw GoogleCloudGax.RequestError.unimplemented
    }

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

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

    public func purgeUserEvents(withPolling: PurgeUserEventsRequest) async throws
      -> any GoogleCloudGax.PollableOperation<PurgeUserEventsResponse>
    {
      try await self.purgeUserEvents(withPolling: withPolling, options: .init())
    }

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

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

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

    public func importUserEvents(withPolling: ImportUserEventsRequest) async throws
      -> any GoogleCloudGax.PollableOperation<ImportUserEventsResponse>
    {
      try await self.importUserEvents(withPolling: withPolling, options: .init())
    }

    public func importUserEvents(
      withPolling: ImportUserEventsRequest, options: GoogleCloudGax.RequestOptions
    ) async throws -> any GoogleCloudGax.PollableOperation<ImportUserEventsResponse> {
      let poll = {
        () async throws -> GoogleCloudGax._PollableOperationImpl<ImportUserEventsResponse>.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)
    }

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

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

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