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

import Foundation
import NitroModules

/**
 * Helper class for converting instances of `HybridWebSocketManagerSpecCxx` from- and to unsafe pointers.
 * This is useful to pass Swift classes to C++, without having to strongly type the C++ function signature.
 * The actual Swift type can be included in the .cpp file, without having to forward-declare anything in .hpp.
 */
public final class HybridWebSocketManagerSpecCxxUnsafe {
  /**
   * Casts a `HybridWebSocketManagerSpecCxx` instance to a retained unsafe raw pointer.
   * This acquires one additional strong reference on the object!
   */
  public static func toUnsafe(_ instance: HybridWebSocketManagerSpecCxx) -> UnsafeMutableRawPointer {
    return Unmanaged.passRetained(instance).toOpaque()
  }

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

/**
 * A class implementation that bridges HybridWebSocketManagerSpec 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++
 */
public class HybridWebSocketManagerSpecCxx {
  /**
   * The Swift <> C++ bridge's namespace (`margelo::nitro::websocket::bridge::swift`)
   * from `FastWebSocket-Swift-Cxx-Bridge.hpp`.
   * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
   */
  public typealias bridge = margelo.nitro.websocket.bridge.swift

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

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

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

  /**
   * Contains a (weak) reference to the C++ HybridObject to cache it.
   */
  public var hybridContext: margelo.nitro.HybridContext {
    @inline(__always)
    get {
      return self.__implementation.hybridContext
    }
    @inline(__always)
    set {
      self.__implementation.hybridContext = newValue
    }
  }

  /**
   * 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 self.__implementation.memorySize
  }

  // Properties
  

  // Methods
  @inline(__always)
  public func create(url: std.string, protocols: bridge.std__vector_std__string_) -> bridge.std__shared_ptr_margelo__nitro__websocket__HybridWebSocketSpec_ {
    do {
      let __result = try self.__implementation.create(url: String(url), protocols: protocols.map({ __item in String(__item) }))
      return { () -> bridge.std__shared_ptr_margelo__nitro__websocket__HybridWebSocketSpec_ in
        let __cxxWrapped = HybridWebSocketSpecCxx(__result)
        let __pointer = HybridWebSocketSpecCxxUnsafe.toUnsafe(__cxxWrapped)
        return bridge.create_std__shared_ptr_margelo__nitro__websocket__HybridWebSocketSpec_(__pointer)
      }()
    } catch {
      let __message = "\(error.localizedDescription)"
      fatalError("Swift errors can currently not be propagated to C++! See https://github.com/swiftlang/swift/issues/75290 (Error: \(__message))")
    }
  }
}
