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

import Foundation
import NitroModules

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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridLLMSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridLLMSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridLLMSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridLLMSpec_(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
  }

  /**
   * 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 isLoaded: Bool {
    @inline(__always)
    get {
      return self.__implementation.isLoaded
    }
  }
  
  public final var isGenerating: Bool {
    @inline(__always)
    get {
      return self.__implementation.isGenerating
    }
  }
  
  public final var modelId: std.string {
    @inline(__always)
    get {
      return std.string(self.__implementation.modelId)
    }
  }
  
  public final var debug: Bool {
    @inline(__always)
    get {
      return self.__implementation.debug
    }
    @inline(__always)
    set {
      self.__implementation.debug = newValue
    }
  }
  
  public final var systemPrompt: std.string {
    @inline(__always)
    get {
      return std.string(self.__implementation.systemPrompt)
    }
    @inline(__always)
    set {
      self.__implementation.systemPrompt = String(newValue)
    }
  }
  
  public final var maxTokens: Double {
    @inline(__always)
    get {
      return self.__implementation.maxTokens
    }
    @inline(__always)
    set {
      self.__implementation.maxTokens = newValue
    }
  }
  
  public final var temperature: Double {
    @inline(__always)
    get {
      return self.__implementation.temperature
    }
    @inline(__always)
    set {
      self.__implementation.temperature = newValue
    }
  }
  
  public final var enableThinking: Bool {
    @inline(__always)
    get {
      return self.__implementation.enableThinking
    }
    @inline(__always)
    set {
      self.__implementation.enableThinking = newValue
    }
  }

  // Methods
  @inline(__always)
  public final func load(modelId: std.string, options: bridge.std__optional_LLMLoadOptions_) -> bridge.Result_std__shared_ptr_Promise_void___ {
    do {
      let __result = try self.__implementation.load(modelId: String(modelId), options: options.value)
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
        let __promise = bridge.create_std__shared_ptr_Promise_void__()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
        __result
          .then({ __result in __promiseHolder.resolve() })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func generate(prompt: std.string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
    do {
      let __result = try self.__implementation.generate(prompt: String(prompt))
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
        __result
          .then({ __result in __promiseHolder.resolve(std.string(__result)) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func stream(prompt: std.string, onToken: bridge.Func_void_std__string, onToolCall: bridge.std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
    do {
      let __result = try self.__implementation.stream(prompt: String(prompt), onToken: { () -> (String) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__string(onToken)
        return { (__token: String) -> Void in
          __wrappedFunction.call(std.string(__token))
        }
      }(), onToolCall: { () -> ((_ toolName: String, _ args: String) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______(onToolCall) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_std__string_____toolName_____const_std__string_____args______(onToolCall)
          return { () -> (String, String) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_std__string_std__string(__unwrapped)
            return { (__toolName: String, __args: String) -> Void in
              __wrappedFunction.call(std.string(__toolName), std.string(__args))
            }
          }()
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
        __result
          .then({ __result in __promiseHolder.resolve(std.string(__result)) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func streamWithEvents(prompt: std.string, onEvent: bridge.Func_void_std__string) -> bridge.Result_std__shared_ptr_Promise_std__string___ {
    do {
      let __result = try self.__implementation.streamWithEvents(prompt: String(prompt), onEvent: { () -> (String) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__string(onEvent)
        return { (__eventJson: String) -> Void in
          __wrappedFunction.call(std.string(__eventJson))
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__string__ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__string__()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__string__(__promise)
        __result
          .then({ __result in __promiseHolder.resolve(std.string(__result)) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__string___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func stop() -> bridge.Result_void_ {
    do {
      try self.__implementation.stop()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func unload() -> bridge.Result_void_ {
    do {
      try self.__implementation.unload()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getLastGenerationStats() -> bridge.Result_GenerationStats_ {
    do {
      let __result = try self.__implementation.getLastGenerationStats()
      let __resultCpp = __result
      return bridge.create_Result_GenerationStats_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_GenerationStats_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getHistory() -> bridge.Result_std__vector_LLMMessage__ {
    do {
      let __result = try self.__implementation.getHistory()
      let __resultCpp = { () -> bridge.std__vector_LLMMessage_ in
        var __vector = bridge.create_std__vector_LLMMessage_(__result.count)
        for __item in __result {
          __vector.push_back(__item)
        }
        return __vector
      }()
      return bridge.create_Result_std__vector_LLMMessage__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__vector_LLMMessage__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func clearHistory() -> bridge.Result_void_ {
    do {
      try self.__implementation.clearHistory()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
}
