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

import Foundation
import NitroModules

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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<margelo::nitro::healthkit::HybridStateOfMindModuleSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_margelo__nitro__healthkit__HybridStateOfMindModuleSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if cachedCxxPart.__convertToBool() {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_margelo__nitro__healthkit__HybridStateOfMindModuleSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_margelo__nitro__healthkit__HybridStateOfMindModuleSpec_(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
  }

  // Properties
  

  // Methods
  @inline(__always)
  public final func queryStateOfMindSamples(options: bridge.std__optional_QueryOptionsWithSortOrder_) -> bridge.Result_std__shared_ptr_Promise_std__vector_StateOfMindSample____ {
    do {
      let __result = try self.__implementation.queryStateOfMindSamples(options: { () -> QueryOptionsWithSortOrder? in
        if let __unwrapped = options.value {
          return __unwrapped
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__vector_StateOfMindSample___ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__vector_StateOfMindSample___()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__vector_StateOfMindSample___(__promise)
        __result
          .then({ __result in __promiseHolder.resolve({ () -> bridge.std__vector_StateOfMindSample_ in
              var __vector = bridge.create_std__vector_StateOfMindSample_(__result.count)
              for __item in __result {
                __vector.push_back(__item)
              }
              return __vector
            }()) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__vector_StateOfMindSample____(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__vector_StateOfMindSample____(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func saveStateOfMindSample(date: margelo.nitro.chrono_time, kind: Int32, valence: Double, labels: bridge.std__vector_StateOfMindLabel_, associations: bridge.std__vector_StateOfMindAssociation_, metadata: bridge.std__optional_std__shared_ptr_AnyMap__) -> bridge.Result_std__shared_ptr_Promise_bool___ {
    do {
      let __result = try self.__implementation.saveStateOfMindSample(date: Date(fromChrono: date), kind: margelo.nitro.healthkit.StateOfMindKind(rawValue: kind)!, valence: valence, labels: labels.map({ __item in __item }), associations: associations.map({ __item in __item }), metadata: { () -> AnyMap? in
        if let __unwrapped = metadata.value {
          return AnyMap(withCppPart: __unwrapped)
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_bool__ in
        let __promise = bridge.create_std__shared_ptr_Promise_bool__()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_bool__(__promise)
        __result
          .then({ __result in __promiseHolder.resolve(__result) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_bool___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_bool___(__exceptionPtr)
    }
  }
}
