///
/// HybridNetSocketDriverSpec_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 HybridNetSocketDriverSpec 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 HybridNetSocketDriverSpec_cxx {
  /**
   * The Swift <> C++ bridge's namespace (`margelo::nitro::net::bridge::swift`)
   * from `RustCNet-Swift-Cxx-Bridge.hpp`.
   * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
   */
  public typealias bridge = margelo.nitro.net.bridge.swift

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

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

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

  /**
   * Get the actual `HybridNetSocketDriverSpec` instance this class wraps.
   */
  @inline(__always)
  public func getHybridNetSocketDriverSpec() -> any HybridNetSocketDriverSpec {
    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 `HybridNetSocketDriverSpec_cxx`.
   * The pointer has to be a retained opaque `Unmanaged<HybridNetSocketDriverSpec_cxx>`.
   * This removes one strong reference from the object!
   */
  public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridNetSocketDriverSpec_cxx {
    return Unmanaged<HybridNetSocketDriverSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
  }

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridNetSocketDriverSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridNetSocketDriverSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridNetSocketDriverSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridNetSocketDriverSpec_(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: HybridNetSocketDriverSpec_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
  public final var id: Double {
    @inline(__always)
    get {
      return self.__implementation.id
    }
  }
  
  public final var onEvent: bridge.Func_void_double_std__shared_ptr_ArrayBuffer_ {
    @inline(__always)
    get {
      return { () -> bridge.Func_void_double_std__shared_ptr_ArrayBuffer_ in
        let __closureWrapper = Func_void_double_std__shared_ptr_ArrayBuffer_(self.__implementation.onEvent)
        return bridge.create_Func_void_double_std__shared_ptr_ArrayBuffer_(__closureWrapper.toUnsafe())
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onEvent = { () -> (Double, ArrayBuffer) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_double_std__shared_ptr_ArrayBuffer_(newValue)
        return { (__event: Double, __data: ArrayBuffer) -> Void in
          __wrappedFunction.call(__event, __data)
        }
      }()
    }
  }

  // Methods
  @inline(__always)
  public final func connect(host: std.string, port: Double) -> bridge.Result_void_ {
    do {
      try self.__implementation.connect(host: String(host), port: port)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func connectTLS(host: std.string, port: Double, serverName: bridge.std__optional_std__string_, rejectUnauthorized: bridge.std__optional_bool_) -> bridge.Result_void_ {
    do {
      try self.__implementation.connectTLS(host: String(host), port: port, serverName: { () -> String? in
        if bridge.has_value_std__optional_std__string_(serverName) {
          let __unwrapped = bridge.get_std__optional_std__string_(serverName)
          return String(__unwrapped)
        } else {
          return nil
        }
      }(), rejectUnauthorized: { () -> Bool? in
        if bridge.has_value_std__optional_bool_(rejectUnauthorized) {
          let __unwrapped = bridge.get_std__optional_bool_(rejectUnauthorized)
          return __unwrapped
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func connectTLSWithContext(host: std.string, port: Double, serverName: bridge.std__optional_std__string_, rejectUnauthorized: bridge.std__optional_bool_, secureContextId: bridge.std__optional_double_) -> bridge.Result_void_ {
    do {
      try self.__implementation.connectTLSWithContext(host: String(host), port: port, serverName: { () -> String? in
        if bridge.has_value_std__optional_std__string_(serverName) {
          let __unwrapped = bridge.get_std__optional_std__string_(serverName)
          return String(__unwrapped)
        } else {
          return nil
        }
      }(), rejectUnauthorized: { () -> Bool? in
        if bridge.has_value_std__optional_bool_(rejectUnauthorized) {
          let __unwrapped = bridge.get_std__optional_bool_(rejectUnauthorized)
          return __unwrapped
        } else {
          return nil
        }
      }(), secureContextId: { () -> Double? in
        if bridge.has_value_std__optional_double_(secureContextId) {
          let __unwrapped = bridge.get_std__optional_double_(secureContextId)
          return __unwrapped
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getAuthorizationError() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getAuthorizationError()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getProtocol() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getProtocol()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getCipher() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getCipher()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getALPN() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getALPN()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getPeerCertificateJSON() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getPeerCertificateJSON()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getEphemeralKeyInfo() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getEphemeralKeyInfo()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getSharedSigalgs() -> bridge.Result_std__optional_std__string__ {
    do {
      let __result = try self.__implementation.getSharedSigalgs()
      let __resultCpp = { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__string__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__string__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func isSessionReused() -> bridge.Result_bool_ {
    do {
      let __result = try self.__implementation.isSessionReused()
      let __resultCpp = __result
      return bridge.create_Result_bool_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_bool_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getSession() -> bridge.Result_std__optional_std__shared_ptr_ArrayBuffer___ {
    do {
      let __result = try self.__implementation.getSession()
      let __resultCpp = { () -> bridge.std__optional_std__shared_ptr_ArrayBuffer__ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__shared_ptr_ArrayBuffer__(__unwrappedValue.getArrayBuffer())
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__shared_ptr_ArrayBuffer___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__shared_ptr_ArrayBuffer___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func setSession(session: ArrayBuffer) -> bridge.Result_void_ {
    do {
      try self.__implementation.setSession(session: session)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func connectUnix(path: std.string) -> bridge.Result_void_ {
    do {
      try self.__implementation.connectUnix(path: String(path))
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func connectUnixTLS(path: std.string, serverName: bridge.std__optional_std__string_, rejectUnauthorized: bridge.std__optional_bool_) -> bridge.Result_void_ {
    do {
      try self.__implementation.connectUnixTLS(path: String(path), serverName: { () -> String? in
        if bridge.has_value_std__optional_std__string_(serverName) {
          let __unwrapped = bridge.get_std__optional_std__string_(serverName)
          return String(__unwrapped)
        } else {
          return nil
        }
      }(), rejectUnauthorized: { () -> Bool? in
        if bridge.has_value_std__optional_bool_(rejectUnauthorized) {
          let __unwrapped = bridge.get_std__optional_bool_(rejectUnauthorized)
          return __unwrapped
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func connectUnixTLSWithContext(path: std.string, serverName: bridge.std__optional_std__string_, rejectUnauthorized: bridge.std__optional_bool_, secureContextId: bridge.std__optional_double_) -> bridge.Result_void_ {
    do {
      try self.__implementation.connectUnixTLSWithContext(path: String(path), serverName: { () -> String? in
        if bridge.has_value_std__optional_std__string_(serverName) {
          let __unwrapped = bridge.get_std__optional_std__string_(serverName)
          return String(__unwrapped)
        } else {
          return nil
        }
      }(), rejectUnauthorized: { () -> Bool? in
        if bridge.has_value_std__optional_bool_(rejectUnauthorized) {
          let __unwrapped = bridge.get_std__optional_bool_(rejectUnauthorized)
          return __unwrapped
        } else {
          return nil
        }
      }(), secureContextId: { () -> Double? in
        if bridge.has_value_std__optional_double_(secureContextId) {
          let __unwrapped = bridge.get_std__optional_double_(secureContextId)
          return __unwrapped
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func write(data: ArrayBuffer) -> bridge.Result_void_ {
    do {
      try self.__implementation.write(data: data)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func pause() -> bridge.Result_void_ {
    do {
      try self.__implementation.pause()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func resume() -> bridge.Result_void_ {
    do {
      try self.__implementation.resume()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func shutdown() -> bridge.Result_void_ {
    do {
      try self.__implementation.shutdown()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func setTimeout(timeout: Double) -> bridge.Result_void_ {
    do {
      try self.__implementation.setTimeout(timeout: timeout)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func destroy() -> bridge.Result_void_ {
    do {
      try self.__implementation.destroy()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func resetAndDestroy() -> bridge.Result_void_ {
    do {
      try self.__implementation.resetAndDestroy()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func enableKeylog() -> bridge.Result_void_ {
    do {
      try self.__implementation.enableKeylog()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func enableTrace() -> bridge.Result_void_ {
    do {
      try self.__implementation.enableTrace()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func exportKeyingMaterial(length: Double, label: std.string, context: bridge.std__optional_std__shared_ptr_ArrayBuffer__) -> bridge.Result_std__optional_std__shared_ptr_ArrayBuffer___ {
    do {
      let __result = try self.__implementation.exportKeyingMaterial(length: length, label: String(label), context: { () -> ArrayBuffer? in
        if bridge.has_value_std__optional_std__shared_ptr_ArrayBuffer__(context) {
          let __unwrapped = bridge.get_std__optional_std__shared_ptr_ArrayBuffer__(context)
          return ArrayBuffer(__unwrapped)
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__optional_std__shared_ptr_ArrayBuffer__ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__shared_ptr_ArrayBuffer__(__unwrappedValue.getArrayBuffer())
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__shared_ptr_ArrayBuffer___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__shared_ptr_ArrayBuffer___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func setNoDelay(enable: Bool) -> bridge.Result_void_ {
    do {
      try self.__implementation.setNoDelay(enable: enable)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func setKeepAlive(enable: Bool, delay: Double) -> bridge.Result_void_ {
    do {
      try self.__implementation.setKeepAlive(enable: enable, delay: delay)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getLocalAddress() -> bridge.Result_std__string_ {
    do {
      let __result = try self.__implementation.getLocalAddress()
      let __resultCpp = std.string(__result)
      return bridge.create_Result_std__string_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__string_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getRemoteAddress() -> bridge.Result_std__string_ {
    do {
      let __result = try self.__implementation.getRemoteAddress()
      let __resultCpp = std.string(__result)
      return bridge.create_Result_std__string_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__string_(__exceptionPtr)
    }
  }
}
