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

#include "HybridLinearGradientViewComponent.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 HybridLinearGradientViewComponentName[] = "LinearGradientView";

  HybridLinearGradientViewProps::HybridLinearGradientViewProps(const react::PropsParserContext& context,
                                                               const HybridLinearGradientViewProps& 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("LinearGradientView.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("LinearGradientView.positions: ") + exc.what());
      }
    }()),
    start([&]() -> CachedProp<std::optional<Vector>> {
      try {
        const react::RawValue* rawValue = rawProps.at("start", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.start;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<Vector>>::fromRawValue(*runtime, value, sourceProps.start);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("LinearGradientView.start: ") + exc.what());
      }
    }()),
    end([&]() -> CachedProp<std::optional<Vector>> {
      try {
        const react::RawValue* rawValue = rawProps.at("end", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.end;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<Vector>>::fromRawValue(*runtime, value, sourceProps.end);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("LinearGradientView.end: ") + exc.what());
      }
    }()),
    angle([&]() -> CachedProp<std::optional<double>> {
      try {
        const react::RawValue* rawValue = rawProps.at("angle", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.angle;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<double>>::fromRawValue(*runtime, value, sourceProps.angle);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("LinearGradientView.angle: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridLinearGradientViewSpec>& /* 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<HybridLinearGradientViewSpec>& /* ref */)>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, "f"), sourceProps.hybridRef);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("LinearGradientView.hybridRef: ") + exc.what());
      }
    }()) { }

  HybridLinearGradientViewProps::HybridLinearGradientViewProps(const HybridLinearGradientViewProps& other):
    react::ViewProps(),
    colors(other.colors),
    positions(other.positions),
    start(other.start),
    end(other.end),
    angle(other.angle),
    hybridRef(other.hybridRef) { }

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

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

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

#ifdef ANDROID
  void HybridLinearGradientViewComponentDescriptor::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<HybridLinearGradientViewShadowNode&>(shadowNode);
    const HybridLinearGradientViewProps& props = concreteShadowNode.getConcreteProps();
    HybridLinearGradientViewState state;
    state.setProps(props);
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

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