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

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

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

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

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

  /**
   * Gets (or creates) the C++ part of this Hybrid Object.
   * The C++ part is a `std::shared_ptr<HybridSweepGradientViewSpec>`.
   */
  public func getCxxPart() -> bridge.std__shared_ptr_HybridSweepGradientViewSpec_ {
    let cachedCxxPart = self.__cxxPart.lock()
    if Bool(fromCxx: cachedCxxPart) {
      return cachedCxxPart
    } else {
      let newCxxPart = bridge.create_std__shared_ptr_HybridSweepGradientViewSpec_(self.toUnsafe())
      __cxxPart = bridge.weakify_std__shared_ptr_HybridSweepGradientViewSpec_(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
  }

  /**
   * 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 colors: bridge.std__vector_double_ {
    @inline(__always)
    get {
      return { () -> bridge.std__vector_double_ in
        var __vector = bridge.create_std__vector_double_(self.__implementation.colors.count)
        for __item in self.__implementation.colors {
          __vector.push_back(__item)
        }
        return __vector
      }()
    }
    @inline(__always)
    set {
      self.__implementation.colors = newValue.map({ __item in __item })
    }
  }
  
  public final var positions: bridge.std__optional_std__vector_double__ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_std__vector_double__ in
        if let __unwrappedValue = self.__implementation.positions {
          return bridge.create_std__optional_std__vector_double__({ () -> bridge.std__vector_double_ in
            var __vector = bridge.create_std__vector_double_(__unwrappedValue.count)
            for __item in __unwrappedValue {
              __vector.push_back(__item)
            }
            return __vector
          }())
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.positions = { () -> [Double]? in
        if bridge.has_value_std__optional_std__vector_double__(newValue) {
          let __unwrapped = bridge.get_std__optional_std__vector_double__(newValue)
          return __unwrapped.map({ __item in __item })
        } else {
          return nil
        }
      }()
    }
  }
  
  public final var center: bridge.std__optional_Vector_ {
    @inline(__always)
    get {
      return { () -> bridge.std__optional_Vector_ in
        if let __unwrappedValue = self.__implementation.center {
          return bridge.create_std__optional_Vector_(__unwrappedValue)
        } else {
          return .init()
        }
      }()
    }
    @inline(__always)
    set {
      self.__implementation.center = newValue.value
    }
  }

  // Methods
  @inline(__always)
  public final func update(colors: bridge.std__optional_std__variant_nitro__NullType__std__vector_double___, positions: bridge.std__optional_std__vector_double__, center: bridge.std__optional_Vector_) -> bridge.Result_void_ {
    do {
      try self.__implementation.update(colors: { () -> Variant_NullType__Double_? in
        if bridge.has_value_std__optional_std__variant_nitro__NullType__std__vector_double___(colors) {
          let __unwrapped = bridge.get_std__optional_std__variant_nitro__NullType__std__vector_double___(colors)
          return { () -> Variant_NullType__Double_ in
            let __variant = bridge.std__variant_nitro__NullType__std__vector_double__(__unwrapped)
            switch __variant.index() {
              case 0:
                let __actual = __variant.get_0()
                return .first(NullType.null)
              case 1:
                let __actual = __variant.get_1()
                return .second(__actual.map({ __item in __item }))
              default:
                fatalError("Variant can never have index \(__variant.index())!")
            }
          }()
        } else {
          return nil
        }
      }(), positions: { () -> [Double]? in
        if bridge.has_value_std__optional_std__vector_double__(positions) {
          let __unwrapped = bridge.get_std__optional_std__vector_double__(positions)
          return __unwrapped.map({ __item in __item })
        } else {
          return nil
        }
      }(), center: center.value)
      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()
  }
}
