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

#include "HybridFrameRendererViewComponent.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::camera::views {

  extern const char HybridFrameRendererViewComponentName[] = "FrameRendererView";

  HybridFrameRendererViewProps::HybridFrameRendererViewProps(const react::PropsParserContext& context,
                                                             const HybridFrameRendererViewProps& sourceProps,
                                                             const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    renderer([&]() -> CachedProp<std::optional<std::shared_ptr<HybridFrameRendererSpec>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("renderer", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.renderer;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::shared_ptr<HybridFrameRendererSpec>>>::fromRawValue(*runtime, value, sourceProps.renderer);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("FrameRendererView.renderer: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridFrameRendererViewSpec>& /* 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<HybridFrameRendererViewSpec>& /* 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("FrameRendererView.hybridRef: ") + exc.what());
      }
    }()) { }

  bool HybridFrameRendererViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("renderer"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

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

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

#ifdef ANDROID
  void HybridFrameRendererViewComponentDescriptor::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<HybridFrameRendererViewShadowNode&>(shadowNode);
    const std::shared_ptr<const HybridFrameRendererViewProps>& constProps = concreteShadowNode.getConcreteSharedProps();
    const std::shared_ptr<HybridFrameRendererViewProps>& props = std::const_pointer_cast<HybridFrameRendererViewProps>(constProps);
    HybridFrameRendererViewState state{props};
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

} // namespace margelo::nitro::camera::views
