///
/// HybridUrlRequestBuilderSpec_cxx.swift
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///

import NitroModules

/**
 * A class implementation that bridges HybridUrlRequestBuilderSpec over to C++.
 * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
 *
 * Also, some Swift types need to be bridged with special handling:
 * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
 * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
 * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
 */
open class HybridUrlRequestBuilderSpec_cxx {
  /**
   * The Swift <> C++ bridge's namespace (`margelo::nitro::nitrofetch::bridge::swift`)
   * from `NitroFetch-Swift-Cxx-Bridge.hpp`.
   * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
   */
  public typealias bridge = margelo.nitro.nitrofetch.bridge.swift

  /**
   * Holds an instance of the `HybridUrlRequestBuilderSpec` Swift protocol.
   */
  private var __implementation: any HybridUrlRequestBuilderSpec

  /**
   * Holds a weak pointer to the C++ class that wraps the Swift class.
   */
  private var __cxxPart: bridge.std__weak_ptr_HybridUrlRequestBuilderSpec_

  /**
   * Create a new `HybridUrlRequestBuilderSpec_cxx` that wraps the given `HybridUrlRequestBuilderSpec`.
   * All properties and methods bridge to C++ types.
   */
  public init(_ implementation: any HybridUrlRequestBuilderSpec) {
    self.__implementation = implementation
    self.__cxxPart = .init()
    /* no base class */
  }

  /**
   * Get the actual `HybridUrlRequestBuilderSpec` instance this class wraps.
   */
  @inline(__always)
  public func getHybridUrlRequestBuilderSpec() -> any HybridUrlRequestBuilderSpec {
    return __implementation
  }

  /**
   * Casts this instance to a retained unsafe raw pointer.
   * This acquires one additional strong reference on the object!
   */
  public func toUnsafe() -> UnsafeMutableRawPointer {
    return Unmanaged.passRetained(self).toOpaque()
  }

  /**
   * Casts an unsafe pointer to a `HybridUrlRequestBuilderSpec_cxx`.
   * The pointer has to be a retained opaque `Unmanaged<HybridUrlRequestBuilderSpec_cxx>`.
   * This removes one strong reference from the object!
   */
  public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridUrlRequestBuilderSpec_cxx {
    return Unmanaged<HybridUrlRequestBuilderSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
  }

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridUrlRequestBuilderSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridUrlRequestBuilderSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridUrlRequestBuilderSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridUrlRequestBuilderSpec_(newCxxPart)
      return newCxxPart
    }
  }

  

  /**
   * Get the memory size of the Swift class (plus size of any other allocations)
   * so the JS VM can properly track it and garbage-collect the JS object if needed.
   */
  @inline(__always)
  public var memorySize: Int {
    return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
  }

  /**
   * Compares this object with the given [other] object for reference equality.
   */
  @inline(__always)
  public func equals(other: HybridUrlRequestBuilderSpec_cxx) -> Bool {
    return self.__implementation === other.__implementation
  }

  /**
   * Call dispose() on the Swift class.
   * This _may_ be called manually from JS.
   */
  @inline(__always)
  public func dispose() {
    self.__implementation.dispose()
  }

  /**
   * Call toString() on the Swift class.
   */
  @inline(__always)
  public func toString() -> String {
    return self.__implementation.toString()
  }

  // Properties
  

  // Methods
  @inline(__always)
  public final func setHttpMethod(httpMethod: std.string) -> bridge.Result_void_ {
    do {
      try self.__implementation.setHttpMethod(httpMethod: String(httpMethod))
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func addHeader(name: std.string, value: std.string) -> bridge.Result_void_ {
    do {
      try self.__implementation.addHeader(name: String(name), value: String(value))
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func setUploadBody(body: bridge.std__variant_std__shared_ptr_ArrayBuffer___std__string_) -> bridge.Result_void_ {
    do {
      try self.__implementation.setUploadBody(body: { () -> Variant_ArrayBuffer_String in
        let __variant = body
        switch __variant.index() {
          case 0:
            let __actual = __variant.get_0()
            return .first(ArrayBuffer(__actual))
          case 1:
            let __actual = __variant.get_1()
            return .second(String(__actual))
          default:
            fatalError("Variant can never have index \(__variant.index())!")
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func disableCache() -> bridge.Result_void_ {
    do {
      try self.__implementation.disableCache()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onSucceeded(callback: bridge.Func_void_UrlResponseInfo) -> bridge.Result_void_ {
    do {
      try self.__implementation.onSucceeded(callback: { () -> (UrlResponseInfo) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_UrlResponseInfo(callback)
        return { (__info: UrlResponseInfo) -> Void in
          __wrappedFunction.call(__info)
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onFailed(callback: bridge.Func_void_std__optional_UrlResponseInfo__RequestException) -> bridge.Result_void_ {
    do {
      try self.__implementation.onFailed(callback: { () -> (UrlResponseInfo?, RequestException) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__optional_UrlResponseInfo__RequestException(callback)
        return { (__info: UrlResponseInfo?, __error: RequestException) -> Void in
          __wrappedFunction.call({ () -> bridge.std__optional_UrlResponseInfo_ in
            if let __unwrappedValue = __info {
              return bridge.create_std__optional_UrlResponseInfo_(__unwrappedValue)
            } else {
              return .init()
            }
          }(), __error)
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onCanceled(callback: bridge.Func_void_std__optional_UrlResponseInfo_) -> bridge.Result_void_ {
    do {
      try self.__implementation.onCanceled(callback: { () -> (UrlResponseInfo?) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__optional_UrlResponseInfo_(callback)
        return { (__info: UrlResponseInfo?) -> Void in
          __wrappedFunction.call({ () -> bridge.std__optional_UrlResponseInfo_ in
            if let __unwrappedValue = __info {
              return bridge.create_std__optional_UrlResponseInfo_(__unwrappedValue)
            } else {
              return .init()
            }
          }())
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onRedirectReceived(callback: bridge.Func_void_UrlResponseInfo_std__string) -> bridge.Result_void_ {
    do {
      try self.__implementation.onRedirectReceived(callback: { () -> (UrlResponseInfo, String) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_UrlResponseInfo_std__string(callback)
        return { (__info: UrlResponseInfo, __newLocationUrl: String) -> Void in
          __wrappedFunction.call(__info, std.string(__newLocationUrl))
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onResponseStarted(callback: bridge.Func_void_UrlResponseInfo) -> bridge.Result_void_ {
    do {
      try self.__implementation.onResponseStarted(callback: { () -> (UrlResponseInfo) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_UrlResponseInfo(callback)
        return { (__info: UrlResponseInfo) -> Void in
          __wrappedFunction.call(__info)
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func onReadCompleted(callback: bridge.Func_void_UrlResponseInfo_std__shared_ptr_ArrayBuffer__double) -> bridge.Result_void_ {
    do {
      try self.__implementation.onReadCompleted(callback: { () -> (UrlResponseInfo, ArrayBuffer, Double) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_UrlResponseInfo_std__shared_ptr_ArrayBuffer__double(callback)
        return { (__info: UrlResponseInfo, __byteBuffer: ArrayBuffer, __bytesRead: Double) -> Void in
          __wrappedFunction.call(__info, __byteBuffer, __bytesRead)
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func build() -> bridge.Result_std__shared_ptr_HybridUrlRequestSpec__ {
    do {
      let __result = try self.__implementation.build()
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridUrlRequestSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridUrlRequestSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridUrlRequestSpec__(__exceptionPtr)
    }
  }
}
