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

#include "HybridPreviewViewComponent.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 HybridPreviewViewComponentName[] = "PreviewView";

  HybridPreviewViewProps::HybridPreviewViewProps(const react::PropsParserContext& context,
                                                 const HybridPreviewViewProps& sourceProps,
                                                 const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    previewOutput([&]() -> CachedProp<std::optional<std::shared_ptr<HybridCameraPreviewOutputSpec>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("previewOutput", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.previewOutput;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::shared_ptr<HybridCameraPreviewOutputSpec>>>::fromRawValue(*runtime, value, sourceProps.previewOutput);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.previewOutput: ") + exc.what());
      }
    }()),
    resizeMode([&]() -> CachedProp<std::optional<PreviewResizeMode>> {
      try {
        const react::RawValue* rawValue = rawProps.at("resizeMode", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.resizeMode;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<PreviewResizeMode>>::fromRawValue(*runtime, value, sourceProps.resizeMode);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.resizeMode: ") + exc.what());
      }
    }()),
    implementationMode([&]() -> CachedProp<std::optional<PreviewImplementationMode>> {
      try {
        const react::RawValue* rawValue = rawProps.at("implementationMode", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.implementationMode;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<PreviewImplementationMode>>::fromRawValue(*runtime, value, sourceProps.implementationMode);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.implementationMode: ") + exc.what());
      }
    }()),
    gestureControllers([&]() -> CachedProp<std::optional<std::vector<std::shared_ptr<HybridGestureControllerSpec>>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("gestureControllers", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.gestureControllers;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::vector<std::shared_ptr<HybridGestureControllerSpec>>>>::fromRawValue(*runtime, value, sourceProps.gestureControllers);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.gestureControllers: ") + exc.what());
      }
    }()),
    onPreviewStarted([&]() -> CachedProp<std::optional<std::function<void()>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("onPreviewStarted", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.onPreviewStarted;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::function<void()>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.onPreviewStarted);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.onPreviewStarted: ") + exc.what());
      }
    }()),
    onPreviewStopped([&]() -> CachedProp<std::optional<std::function<void()>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("onPreviewStopped", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.onPreviewStopped;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::function<void()>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.onPreviewStopped);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("PreviewView.onPreviewStopped: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridPreviewViewSpec>& /* 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<HybridPreviewViewSpec>& /* 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("PreviewView.hybridRef: ") + exc.what());
      }
    }()) { }

  bool HybridPreviewViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("previewOutput"): return true;
      case hashString("resizeMode"): return true;
      case hashString("implementationMode"): return true;
      case hashString("gestureControllers"): return true;
      case hashString("onPreviewStarted"): return true;
      case hashString("onPreviewStopped"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

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

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

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

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