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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridCameraDeviceFactorySpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridCameraDeviceFactorySpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridCameraDeviceFactorySpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridCameraDeviceFactorySpec_(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: HybridCameraDeviceFactorySpec_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 cameraDevices: bridge.std__vector_std__shared_ptr_HybridCameraDeviceSpec__ {
    @inline(__always)
    get {
      return { () -> bridge.std__vector_std__shared_ptr_HybridCameraDeviceSpec__ in
        var __vector = bridge.create_std__vector_std__shared_ptr_HybridCameraDeviceSpec__(self.__implementation.cameraDevices.count)
        for __item in self.__implementation.cameraDevices {
          __vector.push_back({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
            let __cxxWrapped = __item.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        }
        return __vector
      }()
    }
  }
  
  public final var userPreferredCamera: bridge.std__optional_std__shared_ptr_HybridCameraDeviceSpec__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__shared_ptr_HybridCameraDeviceSpec__ in
        if let __unwrappedValue = self.__implementation.userPreferredCamera {
          return bridge.create_std__optional_std__shared_ptr_HybridCameraDeviceSpec__({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
            let __cxxWrapped = __unwrappedValue.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.userPreferredCamera = { () -> (any HybridCameraDeviceSpec)? in
        if bridge.has_value_std__optional_std__shared_ptr_HybridCameraDeviceSpec__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__shared_ptr_HybridCameraDeviceSpec__(newValue)
          return { () -> any HybridCameraDeviceSpec in
            let __unsafePointer = bridge.get_std__shared_ptr_HybridCameraDeviceSpec_(__unwrapped)
            let __instance = HybridCameraDeviceSpec_cxx.fromUnsafe(__unsafePointer)
            return __instance.getHybridCameraDeviceSpec()
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var supportedMultiCamDeviceCombinations: bridge.std__vector_std__vector_std__shared_ptr_HybridCameraDeviceSpec___ {
    @inline(__always)
    get {
      return { () -> bridge.std__vector_std__vector_std__shared_ptr_HybridCameraDeviceSpec___ in
        var __vector = bridge.create_std__vector_std__vector_std__shared_ptr_HybridCameraDeviceSpec___(self.__implementation.supportedMultiCamDeviceCombinations.count)
        for __item in self.__implementation.supportedMultiCamDeviceCombinations {
          __vector.push_back({ () -> bridge.std__vector_std__shared_ptr_HybridCameraDeviceSpec__ in
            var __vector = bridge.create_std__vector_std__shared_ptr_HybridCameraDeviceSpec__(__item.count)
            for __item in __item {
              __vector.push_back({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
                let __cxxWrapped = __item.getCxxWrapper()
                return __cxxWrapped.getCxxPart()
              }())
            }
            return __vector
          }())
        }
        return __vector
      }()
    }
  }

  // Methods
  @inline(__always)
  public final func addOnCameraDevicesChangedListener(listener: bridge.Func_void_std__vector_std__shared_ptr_HybridCameraDeviceSpec__) -> bridge.Result_ListenerSubscription_ {
    do {
      let __result = try self.__implementation.addOnCameraDevicesChangedListener(listener: { () -> ([(any HybridCameraDeviceSpec)]) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__vector_std__shared_ptr_HybridCameraDeviceSpec__(listener)
        return { (__newDevices: [(any HybridCameraDeviceSpec)]) -> Void in
          __wrappedFunction.call({ () -> bridge.std__vector_std__shared_ptr_HybridCameraDeviceSpec__ in
            var __vector = bridge.create_std__vector_std__shared_ptr_HybridCameraDeviceSpec__(__newDevices.count)
            for __item in __newDevices {
              __vector.push_back({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
                let __cxxWrapped = __item.getCxxWrapper()
                return __cxxWrapped.getCxxPart()
              }())
            }
            return __vector
          }())
        }
      }())
      let __resultCpp = __result
      return bridge.create_Result_ListenerSubscription_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_ListenerSubscription_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getCameraForId(id: std.string) -> bridge.Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___ {
    do {
      let __result = try self.__implementation.getCameraForId(id: String(id))
      let __resultCpp = { () -> bridge.std__optional_std__shared_ptr_HybridCameraDeviceSpec__ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__shared_ptr_HybridCameraDeviceSpec__({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
            let __cxxWrapped = __unwrappedValue.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getSupportedExtensions(camera: bridge.std__shared_ptr_HybridCameraDeviceSpec_) -> bridge.Result_std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec_____ {
    do {
      let __result = try self.__implementation.getSupportedExtensions(camera: { () -> any HybridCameraDeviceSpec in
        let __unsafePointer = bridge.get_std__shared_ptr_HybridCameraDeviceSpec_(camera)
        let __instance = HybridCameraDeviceSpec_cxx.fromUnsafe(__unsafePointer)
        return __instance.getHybridCameraDeviceSpec()
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec____ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec____()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec____(__promise)
        __result
          .then({ __result in __promiseHolder.resolve({ () -> bridge.std__vector_std__shared_ptr_HybridCameraExtensionSpec__ in
              var __vector = bridge.create_std__vector_std__shared_ptr_HybridCameraExtensionSpec__(__result.count)
              for __item in __result {
                __vector.push_back({ () -> bridge.std__shared_ptr_HybridCameraExtensionSpec_ in
                  let __cxxWrapped = __item.getCxxWrapper()
                  return __cxxWrapped.getCxxPart()
                }())
              }
              return __vector
            }()) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec_____(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__vector_std__shared_ptr_HybridCameraExtensionSpec_____(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func getDefaultCamera(position: Int32) -> bridge.Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___ {
    do {
      let __result = try self.__implementation.getDefaultCamera(position: margelo.nitro.camera.CameraPosition(rawValue: position)!)
      let __resultCpp = { () -> bridge.std__optional_std__shared_ptr_HybridCameraDeviceSpec__ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__shared_ptr_HybridCameraDeviceSpec__({ () -> bridge.std__shared_ptr_HybridCameraDeviceSpec_ in
            let __cxxWrapped = __unwrappedValue.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridCameraDeviceSpec___(__exceptionPtr)
    }
  }
}
