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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridOneContactUISpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridOneContactUISpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridOneContactUISpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridOneContactUISpec_(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: HybridOneContactUISpec_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 contact: bridge.std__shared_ptr_HybridNativeCNContactSpec_ {
    @inline(__always)
    get {
      return { () -> bridge.std__shared_ptr_HybridNativeCNContactSpec_ in
        let __cxxWrapped = self.__implementation.contact.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
    }
  }

  public final var displayedPropertyKeys: bridge.std__optional_std__vector_std__string__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__vector_std__string__ in
        if let __unwrappedValue = self.__implementation.displayedPropertyKeys {
          return bridge.create_std__optional_std__vector_std__string__({ () -> bridge.std__vector_std__string_ in
            var __vector = bridge.create_std__vector_std__string_(__unwrappedValue.count)
            for __item in __unwrappedValue {
              __vector.push_back(std.string(__item))
            }
            return __vector
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.displayedPropertyKeys = { () -> [String]? in
        if bridge.has_value_std__optional_std__vector_std__string__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__vector_std__string__(newValue)
          return __unwrapped.map({ __item in String(__item) })
        } else {
          return nil
        }
      }()
    }
  }

  public final var alternateName: bridge.std__optional_std__string_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = self.__implementation.alternateName {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.alternateName = { () -> String? in
        if bridge.has_value_std__optional_std__string_(newValue) {
          let __unwrapped = bridge.get_std__optional_std__string_(newValue)
          return String(__unwrapped)
        } else {
          return nil
        }
      }()
    }
  }

  public final var message: bridge.std__optional_std__string_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__string_ in
        if let __unwrappedValue = self.__implementation.message {
          return bridge.create_std__optional_std__string_(std.string(__unwrappedValue))
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.message = { () -> String? in
        if bridge.has_value_std__optional_std__string_(newValue) {
          let __unwrapped = bridge.get_std__optional_std__string_(newValue)
          return String(__unwrapped)
        } else {
          return nil
        }
      }()
    }
  }

  public final var allowsEditing: Bool {
    @inline(__always)
    get {
      return self.__implementation.allowsEditing
    }
    @inline(__always)
    set {
      self.__implementation.allowsEditing = newValue
    }
  }

  public final var allowsActions: Bool {
    @inline(__always)
    get {
      return self.__implementation.allowsActions
    }
    @inline(__always)
    set {
      self.__implementation.allowsActions = newValue
    }
  }

  public final var shouldShowLinkedContacts: Bool {
    @inline(__always)
    get {
      return self.__implementation.shouldShowLinkedContacts
    }
    @inline(__always)
    set {
      self.__implementation.shouldShowLinkedContacts = newValue
    }
  }

  // Methods
  @inline(__always)
  public final func descriptorForRequiredKeys() -> bridge.Result_std__shared_ptr_HybridNativeCNKeyDescriptorSpec__ {
    do {
      let __result = try self.__implementation.descriptorForRequiredKeys()
      let __resultCpp = { () -> bridge.std__shared_ptr_HybridNativeCNKeyDescriptorSpec_ in
        let __cxxWrapped = __result.getCxxWrapper()
        return __cxxWrapped.getCxxPart()
      }()
      return bridge.create_Result_std__shared_ptr_HybridNativeCNKeyDescriptorSpec__(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__shared_ptr_HybridNativeCNKeyDescriptorSpec__(__exceptionPtr)
    }
  }

  @inline(__always)
  public final func configure(mode: Int32, contact: bridge.std__optional_std__shared_ptr_HybridNativeCNContactSpec__) -> bridge.Result_void_ {
    do {
      try self.__implementation.configure(mode: margelo.nitro.one.ContactUIMode(rawValue: mode)!, contact: { () -> (any HybridNativeCNContactSpec)? in
        if bridge.has_value_std__optional_std__shared_ptr_HybridNativeCNContactSpec__(contact) {
          let __unwrapped = bridge.get_std__optional_std__shared_ptr_HybridNativeCNContactSpec__(contact)
          return { () -> any HybridNativeCNContactSpec in
            let __unsafePointer = bridge.get_std__shared_ptr_HybridNativeCNContactSpec_(__unwrapped)
            let __instance = HybridNativeCNContactSpec_cxx.fromUnsafe(__unsafePointer)
            return __instance.getHybridNativeCNContactSpec()
          }()
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }

  @inline(__always)
  public final func getContactStore() -> bridge.Result_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec___ {
    do {
      let __result = try self.__implementation.getContactStore()
      let __resultCpp = { () -> bridge.std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec__ in
        if let __unwrappedValue = __result {
          return bridge.create_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec__({ () -> bridge.std__shared_ptr_HybridNativeCNContactStoreSpec_ in
            let __cxxWrapped = __unwrappedValue.getCxxWrapper()
            return __cxxWrapped.getCxxPart()
          }())
        } else {
          return .init()
        }
      }()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec___(__resultCpp)
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec___(__exceptionPtr)
    }
  }

  @inline(__always)
  public final func setContactStore(store: bridge.std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec__) -> bridge.Result_void_ {
    do {
      try self.__implementation.setContactStore(store: { () -> (any HybridNativeCNContactStoreSpec)? in
        if bridge.has_value_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec__(store) {
          let __unwrapped = bridge.get_std__optional_std__shared_ptr_HybridNativeCNContactStoreSpec__(store)
          return { () -> any HybridNativeCNContactStoreSpec in
            let __unsafePointer = bridge.get_std__shared_ptr_HybridNativeCNContactStoreSpec_(__unwrapped)
            let __instance = HybridNativeCNContactStoreSpec_cxx.fromUnsafe(__unsafePointer)
            return __instance.getHybridNativeCNContactStoreSpec()
          }()
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }

  @inline(__always)
  public final func highlightProperty(withKey: std.string, identifier: bridge.std__optional_std__string_) -> bridge.Result_void_ {
    do {
      try self.__implementation.highlightProperty(withKey: String(withKey), identifier: { () -> String? in
        if bridge.has_value_std__optional_std__string_(identifier) {
          let __unwrapped = bridge.get_std__optional_std__string_(identifier)
          return String(__unwrapped)
        } else {
          return nil
        }
      }())
      return bridge.create_Result_void_()
    } catch (let __error) {
      let __exceptionPtr = __error.toCpp()
      return bridge.create_Result_void_(__exceptionPtr)
    }
  }

  @inline(__always)
  public final func present(animated: Bool, didComplete: bridge.Func_void_std__optional_std__shared_ptr_HybridNativeCNContactSpec__) -> bridge.Result_std__shared_ptr_Promise_void___ {
    do {
      let __result = try self.__implementation.present(animated: animated, didComplete: { () -> ((any HybridNativeCNContactSpec)?) -> Void in
        let __wrappedFunction = bridge.wrap_Func_void_std__optional_std__shared_ptr_HybridNativeCNContactSpec__(didComplete)
        return { (__contact: (any HybridNativeCNContactSpec)?) -> Void in
          __wrappedFunction.call({ () -> bridge.std__optional_std__shared_ptr_HybridNativeCNContactSpec__ in
            if let __unwrappedValue = __contact {
              return bridge.create_std__optional_std__shared_ptr_HybridNativeCNContactSpec__({ () -> bridge.std__shared_ptr_HybridNativeCNContactSpec_ in
                let __cxxWrapped = __unwrappedValue.getCxxWrapper()
                return __cxxWrapped.getCxxPart()
              }())
            } else {
              return .init()
            }
          }())
        }
      }())
      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 dismiss(animated: Bool) -> bridge.Result_std__shared_ptr_Promise_void___ {
    do {
      let __result = try self.__implementation.dismiss(animated: animated)
      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)
    }
  }
}
