///
/// HybridNetDriverSpec_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 HybridNetDriverSpec 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 HybridNetDriverSpec_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 `HybridNetDriverSpec` Swift protocol.
   */
  private var __implementation: any HybridNetDriverSpec

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridNetDriverSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridNetDriverSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridNetDriverSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridNetDriverSpec_(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: HybridNetDriverSpec_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 createSocket(id: bridge.std__optional_std__string_) -> bridge.Result_std__shared_ptr_HybridNetSocketDriverSpec__ {
    do {
      let __result = try self.__implementation.createSocket(id: { () -> String? in
        if bridge.has_value_std__optional_std__string_(id) {
          let __unwrapped = bridge.get_std__optional_std__string_(id)
          return String(__unwrapped)
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridNetSocketDriverSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridNetSocketDriverSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridNetSocketDriverSpec__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func createServer() -> bridge.Result_std__shared_ptr_HybridNetServerDriverSpec__ {
    do {
      let __result = try self.__implementation.createServer()
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridNetServerDriverSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridNetServerDriverSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridNetServerDriverSpec__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func createHttpParser(mode: Double) -> bridge.Result_std__shared_ptr_HybridHttpParserSpec__ {
    do {
      let __result = try self.__implementation.createHttpParser(mode: mode)
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridHttpParserSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridHttpParserSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridHttpParserSpec__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func createSecureContext(cert: std.string, key: std.string, passphrase: bridge.std__optional_std__string_) -> bridge.Result_double_ {
    do {
      let __result = try self.__implementation.createSecureContext(cert: String(cert), key: String(key), passphrase: { () -> String? in
        if bridge.has_value_std__optional_std__string_(passphrase) {
          let __unwrapped = bridge.get_std__optional_std__string_(passphrase)
          return String(__unwrapped)
        } else {
          return nil
        }
      }())
      let __resultCpp = __result
      return bridge.create_Result_double_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_double_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func createEmptySecureContext() -> bridge.Result_double_ {
    do {
      let __result = try self.__implementation.createEmptySecureContext()
      let __resultCpp = __result
      return bridge.create_Result_double_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_double_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func addCACertToSecureContext(scId: Double, ca: std.string) -> bridge.Result_void_ {
    do {
      try self.__implementation.addCACertToSecureContext(scId: scId, ca: String(ca))
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func addContextToSecureContext(scId: Double, hostname: std.string, cert: std.string, key: std.string, passphrase: bridge.std__optional_std__string_) -> bridge.Result_void_ {
    do {
      try self.__implementation.addContextToSecureContext(scId: scId, hostname: String(hostname), cert: String(cert), key: String(key), passphrase: { () -> String? in
        if bridge.has_value_std__optional_std__string_(passphrase) {
          let __unwrapped = bridge.get_std__optional_std__string_(passphrase)
          return String(__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 setPFXToSecureContext(scId: Double, pfx: ArrayBuffer, passphrase: bridge.std__optional_std__string_) -> bridge.Result_void_ {
    do {
      try self.__implementation.setPFXToSecureContext(scId: scId, pfx: pfx, passphrase: { () -> String? in
        if bridge.has_value_std__optional_std__string_(passphrase) {
          let __unwrapped = bridge.get_std__optional_std__string_(passphrase)
          return String(__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 setOCSPResponseToSecureContext(scId: Double, ocsp: ArrayBuffer) -> bridge.Result_void_ {
    do {
      try self.__implementation.setOCSPResponseToSecureContext(scId: scId, ocsp: ocsp)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getTicketKeys(scId: Double) -> bridge.Result_std__optional_std__shared_ptr_ArrayBuffer___ {
    do {
      let __result = try self.__implementation.getTicketKeys(scId: scId)
      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 setTicketKeys(scId: Double, keys: ArrayBuffer) -> bridge.Result_void_ {
    do {
      try self.__implementation.setTicketKeys(scId: scId, keys: keys)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func initWithConfig(config: NetConfig) -> bridge.Result_void_ {
    do {
      try self.__implementation.initWithConfig(config: config)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
}
