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

#include "HybridParticleCanvasViewComponent.hpp"

#include <string>
#include <exception>
#include <utility>
#include <NitroModules/NitroDefines.hpp>
#include <NitroModules/JSIConverter.hpp>
#include <NitroModules/PropNameIDCache.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::particle::views {

  extern const char HybridParticleCanvasViewComponentName[] = "ParticleCanvasView";

  HybridParticleCanvasViewProps::HybridParticleCanvasViewProps(const react::PropsParserContext& context,
                                                               const HybridParticleCanvasViewProps& sourceProps,
                                                               const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    preset([&]() -> CachedProp<std::string> {
      try {
        const react::RawValue* rawValue = rawProps.at("preset", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.preset;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::string>::fromRawValue(*runtime, value, sourceProps.preset);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.preset: ") + exc.what());
      }
    }()),
    count([&]() -> CachedProp<double> {
      try {
        const react::RawValue* rawValue = rawProps.at("count", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.count;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<double>::fromRawValue(*runtime, value, sourceProps.count);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.count: ") + exc.what());
      }
    }()),
    emitterX([&]() -> CachedProp<double> {
      try {
        const react::RawValue* rawValue = rawProps.at("emitterX", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.emitterX;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<double>::fromRawValue(*runtime, value, sourceProps.emitterX);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.emitterX: ") + exc.what());
      }
    }()),
    emitterY([&]() -> CachedProp<double> {
      try {
        const react::RawValue* rawValue = rawProps.at("emitterY", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.emitterY;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<double>::fromRawValue(*runtime, value, sourceProps.emitterY);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.emitterY: ") + exc.what());
      }
    }()),
    loop([&]() -> CachedProp<bool> {
      try {
        const react::RawValue* rawValue = rawProps.at("loop", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.loop;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<bool>::fromRawValue(*runtime, value, sourceProps.loop);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.loop: ") + exc.what());
      }
    }()),
    emitInterval([&]() -> CachedProp<double> {
      try {
        const react::RawValue* rawValue = rawProps.at("emitInterval", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.emitInterval;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<double>::fromRawValue(*runtime, value, sourceProps.emitInterval);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.emitInterval: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridParticleCanvasViewSpec>& /* 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<HybridParticleCanvasViewSpec>& /* ref */)>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.hybridRef);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("ParticleCanvasView.hybridRef: ") + exc.what());
      }
    }()) { }

  bool HybridParticleCanvasViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("preset"): return true;
      case hashString("count"): return true;
      case hashString("emitterX"): return true;
      case hashString("emitterY"): return true;
      case hashString("loop"): return true;
      case hashString("emitInterval"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

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

  std::shared_ptr<const react::Props> HybridParticleCanvasViewComponentDescriptor::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 HybridParticleCanvasViewShadowNode::Props(context, /* & */ rawProps, props);
  }

#ifdef ANDROID
  void HybridParticleCanvasViewComponentDescriptor::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 = static_cast<HybridParticleCanvasViewShadowNode&>(shadowNode);
    const std::shared_ptr<const HybridParticleCanvasViewProps>& constProps = concreteShadowNode.getConcreteSharedProps();
    const std::shared_ptr<HybridParticleCanvasViewProps>& props = std::const_pointer_cast<HybridParticleCanvasViewProps>(constProps);
    HybridParticleCanvasViewState state{props};
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

} // namespace margelo::nitro::particle::views
