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

import NitroModules
import NitroImage

/**
 * A class implementation that bridges HybridPreviewViewSpec 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 HybridPreviewViewSpec_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 `HybridPreviewViewSpec` Swift protocol.
   */
  private var __implementation: any HybridPreviewViewSpec

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridPreviewViewSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridPreviewViewSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridPreviewViewSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridPreviewViewSpec_(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: HybridPreviewViewSpec_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 previewOutput: bridge.std__optional_std__shared_ptr_HybridCameraPreviewOutputSpec__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__shared_ptr_HybridCameraPreviewOutputSpec__ in
        if let __unwrappedValue = self.__implementation.previewOutput {
          return bridge.create_std__optional_std__shared_ptr_HybridCameraPreviewOutputSpec__({ () -> bridge.std__shared_ptr_HybridCameraPreviewOutputSpec_ in
            let __cxxWrapped = __unwrappedValue.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.previewOutput = { () -> (any HybridCameraPreviewOutputSpec)? in
        if bridge.has_value_std__optional_std__shared_ptr_HybridCameraPreviewOutputSpec__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__shared_ptr_HybridCameraPreviewOutputSpec__(newValue)
          return { () -> any HybridCameraPreviewOutputSpec in
            let __unsafePointer = bridge.get_std__shared_ptr_HybridCameraPreviewOutputSpec_(__unwrapped)
            let __instance = HybridCameraPreviewOutputSpec_cxx.fromUnsafe(__unsafePointer)
            return __instance.getHybridCameraPreviewOutputSpec()
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var resizeMode: bridge.std__optional_PreviewResizeMode_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_PreviewResizeMode_ in
        if let __unwrappedValue = self.__implementation.resizeMode {
          return bridge.create_std__optional_PreviewResizeMode_(__unwrappedValue)
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.resizeMode = newValue.value
    }
  }
  
  public final var implementationMode: bridge.std__optional_PreviewImplementationMode_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_PreviewImplementationMode_ in
        if let __unwrappedValue = self.__implementation.implementationMode {
          return bridge.create_std__optional_PreviewImplementationMode_(__unwrappedValue)
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.implementationMode = newValue.value
    }
  }
  
  public final var gestureControllers: bridge.std__optional_std__vector_std__shared_ptr_HybridGestureControllerSpec___ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__vector_std__shared_ptr_HybridGestureControllerSpec___ in
        if let __unwrappedValue = self.__implementation.gestureControllers {
          return bridge.create_std__optional_std__vector_std__shared_ptr_HybridGestureControllerSpec___({ () -> bridge.std__vector_std__shared_ptr_HybridGestureControllerSpec__ in
            var __vector = bridge.create_std__vector_std__shared_ptr_HybridGestureControllerSpec__(__unwrappedValue.count)
            for __item in __unwrappedValue {
              __vector.push_back({ () -> bridge.std__shared_ptr_HybridGestureControllerSpec_ in
                let __cxxWrapped = __item.getCxxWrapper()
                return __cxxWrapped.getCxxPart()
              }())
            }
            return __vector
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.gestureControllers = { () -> [(any HybridGestureControllerSpec)]? in
        if bridge.has_value_std__optional_std__vector_std__shared_ptr_HybridGestureControllerSpec___(newValue) {
          let __unwrapped = bridge.get_std__optional_std__vector_std__shared_ptr_HybridGestureControllerSpec___(newValue)
          return __unwrapped.map({ __item in { () -> any HybridGestureControllerSpec in
            let __unsafePointer = bridge.get_std__shared_ptr_HybridGestureControllerSpec_(__item)
            let __instance = HybridGestureControllerSpec_cxx.fromUnsafe(__unsafePointer)
            return __instance.getHybridGestureControllerSpec()
          }() })
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onPreviewStarted: bridge.std__optional_std__function_void____ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void____ in
        if let __unwrappedValue = self.__implementation.onPreviewStarted {
          return bridge.create_std__optional_std__function_void____({ () -> bridge.Func_void in
            let __closureWrapper = Func_void(__unwrappedValue)
            return bridge.create_Func_void(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onPreviewStarted = { () -> (() -> Void)? in
        if bridge.has_value_std__optional_std__function_void____(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void____(newValue)
          return { () -> () -> Void in
            let __wrappedFunction = bridge.wrap_Func_void(__unwrapped)
            return { () -> Void in
              __wrappedFunction.call()
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onPreviewStopped: bridge.std__optional_std__function_void____ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void____ in
        if let __unwrappedValue = self.__implementation.onPreviewStopped {
          return bridge.create_std__optional_std__function_void____({ () -> bridge.Func_void in
            let __closureWrapper = Func_void(__unwrappedValue)
            return bridge.create_Func_void(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onPreviewStopped = { () -> (() -> Void)? in
        if bridge.has_value_std__optional_std__function_void____(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void____(newValue)
          return { () -> () -> Void in
            let __wrappedFunction = bridge.wrap_Func_void(__unwrapped)
            return { () -> Void in
              __wrappedFunction.call()
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }

  // Methods
  @inline(__always)
  public final func createMeteringPoint(viewX: Double, viewY: Double, size: bridge.std__optional_double_) -> bridge.Result_std__shared_ptr_HybridMeteringPointSpec__ {
    do {
      let __result = try self.__implementation.createMeteringPoint(viewX: viewX, viewY: viewY, size: { () -> Double? in
        if bridge.has_value_std__optional_double_(size) {
          let __unwrapped = bridge.get_std__optional_double_(size)
          return __unwrapped
        } else {
          return nil
        }
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridMeteringPointSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridMeteringPointSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridMeteringPointSpec__(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func takeSnapshot() -> bridge.Result_std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec____ {
    do {
      let __result = try self.__implementation.takeSnapshot()
      let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec___ in
        let __promise = bridge.create_std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec___()
        let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec___(__promise)
        __result
          .then({ __result in __promiseHolder.resolve({ () -> bridge.std__shared_ptr_margelo__nitro__image__HybridImageSpec_ in
              let __cxxWrapped = __result.getCxxWrapper()
              return __cxxWrapped.getCxxPart()
            }()) })
          .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
        return __promise
      }()
      return bridge.create_Result_std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec____(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_Promise_std__shared_ptr_margelo__nitro__image__HybridImageSpec____(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func convertCameraPointToViewPoint(cameraPoint: Point) -> bridge.Result_Point_ {
    do {
      let __result = try self.__implementation.convertCameraPointToViewPoint(cameraPoint: cameraPoint)
      let __resultCpp = __result
      return bridge.create_Result_Point_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_Point_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func convertViewPointToCameraPoint(viewPoint: Point) -> bridge.Result_Point_ {
    do {
      let __result = try self.__implementation.convertViewPointToCameraPoint(viewPoint: viewPoint)
      let __resultCpp = __result
      return bridge.create_Result_Point_(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_Point_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func convertScannedObjectCoordinatesToViewCoordinates(scannedObject: bridge.std__shared_ptr_HybridScannedObjectSpec_) -> bridge.Result_std__shared_ptr_HybridScannedObjectSpec__ {
    do {
      let __result = try self.__implementation.convertScannedObjectCoordinatesToViewCoordinates(scannedObject: { () -> any HybridScannedObjectSpec in
        let __unsafePointer = bridge.get_std__shared_ptr_HybridScannedObjectSpec_(scannedObject)
        let __instance = HybridScannedObjectSpec_cxx.fromUnsafe(__unsafePointer)
        return __instance.getHybridScannedObjectSpec()
      }())
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridScannedObjectSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridScannedObjectSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridScannedObjectSpec__(__exceptionPtr)
    }
  }
  
  public final func getView() -> UnsafeMutableRawPointer {
    return Unmanaged.passRetained(__implementation.view).toOpaque()
  }
  
  public final func beforeUpdate() {
    __implementation.beforeUpdate()
  }
  
  public final func afterUpdate() {
    __implementation.afterUpdate()
  }
  
  public final func maybePrepareForRecycle() {
    guard let recyclable = __implementation as? any RecyclableView else { return }
    recyclable.prepareForRecycle()
  }
  
  public final func onDropView() {
    __implementation.onDropView()
  }
}
