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

#include "HybridSweepGradientViewComponent.hpp"

#include <string>
#include <exception>
#include <utility>
#include <NitroModules/NitroDefines.hpp>
#include <NitroModules/JSIConverter.hpp>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/ComponentDescriptor.h>
#include <react/renderer/components/view/ViewProps.h>

namespace margelo::nitro::gradient::views {

  extern const char HybridSweepGradientViewComponentName[] = "SweepGradientView";

  HybridSweepGradientViewProps::HybridSweepGradientViewProps(const react::PropsParserContext& context,
                                                             const HybridSweepGradientViewProps& sourceProps,
                                                             const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    colors([&]() -> CachedProp<std::vector<double>> {
      try {
        const react::RawValue* rawValue = rawProps.at("colors", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.colors;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::vector<double>>::fromRawValue(*runtime, value, sourceProps.colors);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("SweepGradientView.colors: ") + exc.what());
      }
    }()),
    positions([&]() -> CachedProp<std::optional<std::vector<double>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("positions", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.positions;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::vector<double>>>::fromRawValue(*runtime, value, sourceProps.positions);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("SweepGradientView.positions: ") + exc.what());
      }
    }()),
    center([&]() -> CachedProp<std::optional<Vector>> {
      try {
        const react::RawValue* rawValue = rawProps.at("center", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.center;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<Vector>>::fromRawValue(*runtime, value, sourceProps.center);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("SweepGradientView.center: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridSweepGradientViewSpec>& /* ref */)>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("hybridRef", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.hybridRef;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridSweepGradientViewSpec>& /* ref */)>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, "f"), sourceProps.hybridRef);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("SweepGradientView.hybridRef: ") + exc.what());
      }
    }()) { }

  HybridSweepGradientViewProps::HybridSweepGradientViewProps(const HybridSweepGradientViewProps& other):
    react::ViewProps(),
    colors(other.colors),
    positions(other.positions),
    center(other.center),
    hybridRef(other.hybridRef) { }

  bool HybridSweepGradientViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("colors"): return true;
      case hashString("positions"): return true;
      case hashString("center"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

  HybridSweepGradientViewComponentDescriptor::HybridSweepGradientViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters)
    : ConcreteComponentDescriptor(parameters,
                                  react::RawPropsParser(/* enableJsiParser */ true)) {}

  std::shared_ptr<const react::Props> HybridSweepGradientViewComponentDescriptor::cloneProps(const react::PropsParserContext& context,
                                                                                             const std::shared_ptr<const react::Props>& props,
                                                                                             react::RawProps rawProps) const {
    // 1. Prepare raw props parser
    rawProps.parse(rawPropsParser_);
    // 2. Copy props with Nitro's cached copy constructor
    return HybridSweepGradientViewShadowNode::Props(context, /* & */ rawProps, props);
  }

#ifdef ANDROID
  void HybridSweepGradientViewComponentDescriptor::adopt(react::ShadowNode& shadowNode) const {
    // This is called immediately after `ShadowNode` is created, cloned or in progress.
    // On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++.
    auto& concreteShadowNode = dynamic_cast<HybridSweepGradientViewShadowNode&>(shadowNode);
    const HybridSweepGradientViewProps& props = concreteShadowNode.getConcreteProps();
    HybridSweepGradientViewState state;
    state.setProps(props);
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

} // namespace margelo::nitro::gradient::views
