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

#include "HybridThorViewComponent.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::thor::views {

  extern const char HybridThorViewComponentName[] = "ThorView";

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

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

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

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

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

} // namespace margelo::nitro::thor::views
