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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridReadiumViewSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridReadiumViewSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridReadiumViewSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridReadiumViewSpec_(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: HybridReadiumViewSpec_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 file: bridge.std__optional_ReadiumFile_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_ReadiumFile_ in
        if let __unwrappedValue = self.__implementation.file {
          return bridge.create_std__optional_ReadiumFile_(__unwrappedValue)
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.file = newValue.value
    }
  }
  
  public final var preferences: bridge.std__optional_Preferences_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_Preferences_ in
        if let __unwrappedValue = self.__implementation.preferences {
          return bridge.create_std__optional_Preferences_(__unwrappedValue)
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.preferences = newValue.value
    }
  }
  
  public final var decorations: bridge.std__optional_std__vector_DecorationGroup__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__vector_DecorationGroup__ in
        if let __unwrappedValue = self.__implementation.decorations {
          return bridge.create_std__optional_std__vector_DecorationGroup__({ () -> bridge.std__vector_DecorationGroup_ in
            var __vector = bridge.create_std__vector_DecorationGroup_(__unwrappedValue.count)
            for __item in __unwrappedValue {
              __vector.push_back(__item)
            }
            return __vector
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.decorations = { () -> [DecorationGroup]? in
        if bridge.has_value_std__optional_std__vector_DecorationGroup__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__vector_DecorationGroup__(newValue)
          return __unwrapped.map({ __item in __item })
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var selectionActions: bridge.std__optional_std__vector_SelectionAction__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__vector_SelectionAction__ in
        if let __unwrappedValue = self.__implementation.selectionActions {
          return bridge.create_std__optional_std__vector_SelectionAction__({ () -> bridge.std__vector_SelectionAction_ in
            var __vector = bridge.create_std__vector_SelectionAction_(__unwrappedValue.count)
            for __item in __unwrappedValue {
              __vector.push_back(__item)
            }
            return __vector
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.selectionActions = { () -> [SelectionAction]? in
        if bridge.has_value_std__optional_std__vector_SelectionAction__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__vector_SelectionAction__(newValue)
          return __unwrapped.map({ __item in __item })
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onLocationChange: bridge.std__optional_std__function_void_const_Locator_____locator______ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void_const_Locator_____locator______ in
        if let __unwrappedValue = self.__implementation.onLocationChange {
          return bridge.create_std__optional_std__function_void_const_Locator_____locator______({ () -> bridge.Func_void_Locator in
            let __closureWrapper = Func_void_Locator(__unwrappedValue)
            return bridge.create_Func_void_Locator(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onLocationChange = { () -> ((_ locator: Locator) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_Locator_____locator______(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_Locator_____locator______(newValue)
          return { () -> (Locator) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_Locator(__unwrapped)
            return { (__locator: Locator) -> Void in
              __wrappedFunction.call(__locator)
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onPublicationReady: bridge.std__optional_std__function_void_const_PublicationReadyEvent_____event______ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void_const_PublicationReadyEvent_____event______ in
        if let __unwrappedValue = self.__implementation.onPublicationReady {
          return bridge.create_std__optional_std__function_void_const_PublicationReadyEvent_____event______({ () -> bridge.Func_void_PublicationReadyEvent in
            let __closureWrapper = Func_void_PublicationReadyEvent(__unwrappedValue)
            return bridge.create_Func_void_PublicationReadyEvent(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onPublicationReady = { () -> ((_ event: PublicationReadyEvent) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_PublicationReadyEvent_____event______(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_PublicationReadyEvent_____event______(newValue)
          return { () -> (PublicationReadyEvent) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_PublicationReadyEvent(__unwrapped)
            return { (__event: PublicationReadyEvent) -> Void in
              __wrappedFunction.call(__event)
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onDecorationActivated: bridge.std__optional_std__function_void_const_DecorationActivatedEvent_____event______ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void_const_DecorationActivatedEvent_____event______ in
        if let __unwrappedValue = self.__implementation.onDecorationActivated {
          return bridge.create_std__optional_std__function_void_const_DecorationActivatedEvent_____event______({ () -> bridge.Func_void_DecorationActivatedEvent in
            let __closureWrapper = Func_void_DecorationActivatedEvent(__unwrappedValue)
            return bridge.create_Func_void_DecorationActivatedEvent(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onDecorationActivated = { () -> ((_ event: DecorationActivatedEvent) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_DecorationActivatedEvent_____event______(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_DecorationActivatedEvent_____event______(newValue)
          return { () -> (DecorationActivatedEvent) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_DecorationActivatedEvent(__unwrapped)
            return { (__event: DecorationActivatedEvent) -> Void in
              __wrappedFunction.call(__event)
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onSelectionChange: bridge.std__optional_std__function_void_const_SelectionEvent_____event______ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void_const_SelectionEvent_____event______ in
        if let __unwrappedValue = self.__implementation.onSelectionChange {
          return bridge.create_std__optional_std__function_void_const_SelectionEvent_____event______({ () -> bridge.Func_void_SelectionEvent in
            let __closureWrapper = Func_void_SelectionEvent(__unwrappedValue)
            return bridge.create_Func_void_SelectionEvent(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onSelectionChange = { () -> ((_ event: SelectionEvent) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_SelectionEvent_____event______(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_SelectionEvent_____event______(newValue)
          return { () -> (SelectionEvent) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_SelectionEvent(__unwrapped)
            return { (__event: SelectionEvent) -> Void in
              __wrappedFunction.call(__event)
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var onSelectionAction: bridge.std__optional_std__function_void_const_SelectionActionEvent_____event______ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__function_void_const_SelectionActionEvent_____event______ in
        if let __unwrappedValue = self.__implementation.onSelectionAction {
          return bridge.create_std__optional_std__function_void_const_SelectionActionEvent_____event______({ () -> bridge.Func_void_SelectionActionEvent in
            let __closureWrapper = Func_void_SelectionActionEvent(__unwrappedValue)
            return bridge.create_Func_void_SelectionActionEvent(__closureWrapper.toUnsafe())
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.onSelectionAction = { () -> ((_ event: SelectionActionEvent) -> Void)? in
        if bridge.has_value_std__optional_std__function_void_const_SelectionActionEvent_____event______(newValue) {
          let __unwrapped = bridge.get_std__optional_std__function_void_const_SelectionActionEvent_____event______(newValue)
          return { () -> (SelectionActionEvent) -> Void in
            let __wrappedFunction = bridge.wrap_Func_void_SelectionActionEvent(__unwrapped)
            return { (__event: SelectionActionEvent) -> Void in
              __wrappedFunction.call(__event)
            }
          }()
        } else {
          return nil
        }
      }()
    }
  }

  // Methods
  @inline(__always)
  public final func goTo(locator: Locator) -> bridge.Result_void_ {
    do {
      try self.__implementation.goTo(locator: locator)
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func goForward() -> bridge.Result_void_ {
    do {
      try self.__implementation.goForward()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func goBackward() -> bridge.Result_void_ {
    do {
      try self.__implementation.goBackward()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }
  
  @inline(__always)
  public final func destroy() -> bridge.Result_void_ {
    do {
      try self.__implementation.destroy()
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__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()
  }
}
