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

#include "JHybridNitroFetchClientSpec.hpp"

// Forward declaration of `NitroResponse` to properly resolve imports.
namespace margelo::nitro::nitrofetch { struct NitroResponse; }
// Forward declaration of `NitroHeader` to properly resolve imports.
namespace margelo::nitro::nitrofetch { struct NitroHeader; }
// Forward declaration of `NitroRequest` to properly resolve imports.
namespace margelo::nitro::nitrofetch { struct NitroRequest; }
// Forward declaration of `NitroRequestMethod` to properly resolve imports.
namespace margelo::nitro::nitrofetch { enum class NitroRequestMethod; }
// Forward declaration of `NitroFormDataPart` to properly resolve imports.
namespace margelo::nitro::nitrofetch { struct NitroFormDataPart; }

#include "NitroResponse.hpp"
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include "JNitroResponse.hpp"
#include <string>
#include "NitroHeader.hpp"
#include <vector>
#include "JNitroHeader.hpp"
#include <optional>
#include <NitroModules/ArrayBuffer.hpp>
#include <NitroModules/JArrayBuffer.hpp>
#include <NitroModules/JUnit.hpp>
#include "NitroRequest.hpp"
#include "JNitroRequest.hpp"
#include "NitroRequestMethod.hpp"
#include "JNitroRequestMethod.hpp"
#include "NitroFormDataPart.hpp"
#include "JNitroFormDataPart.hpp"

namespace margelo::nitro::nitrofetch {

  std::shared_ptr<JHybridNitroFetchClientSpec> JHybridNitroFetchClientSpec::JavaPart::getJHybridNitroFetchClientSpec() {
    auto hybridObject = JHybridObject::JavaPart::getJHybridObject();
    auto castHybridObject = std::dynamic_pointer_cast<JHybridNitroFetchClientSpec>(hybridObject);
    if (castHybridObject == nullptr) [[unlikely]] {
      throw std::runtime_error("Failed to downcast JHybridObject to JHybridNitroFetchClientSpec!");
    }
    return castHybridObject;
  }

  jni::local_ref<JHybridNitroFetchClientSpec::CxxPart::jhybriddata> JHybridNitroFetchClientSpec::CxxPart::initHybrid(jni::alias_ref<jhybridobject> jThis) {
    return makeCxxInstance(jThis);
  }

  std::shared_ptr<JHybridObject> JHybridNitroFetchClientSpec::CxxPart::createHybridObject(const jni::local_ref<JHybridObject::JavaPart>& javaPart) {
    auto castJavaPart = jni::dynamic_ref_cast<JHybridNitroFetchClientSpec::JavaPart>(javaPart);
    if (castJavaPart == nullptr) [[unlikely]] {
      throw std::runtime_error("Failed to cast JHybridObject::JavaPart to JHybridNitroFetchClientSpec::JavaPart!");
    }
    return std::make_shared<JHybridNitroFetchClientSpec>(castJavaPart);
  }

  void JHybridNitroFetchClientSpec::CxxPart::registerNatives() {
    registerHybrid({
      makeNativeMethod("initHybrid", JHybridNitroFetchClientSpec::CxxPart::initHybrid),
    });
  }

  // Properties
  

  // Methods
  std::shared_ptr<Promise<NitroResponse>> JHybridNitroFetchClientSpec::request(const NitroRequest& req) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JNitroRequest> /* req */)>("request");
    auto __result = method(_javaPart, JNitroRequest::fromCpp(req));
    return [&]() {
      auto __promise = Promise<NitroResponse>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JNitroResponse>(__boxedResult);
        __promise->resolve(__result->toCpp());
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  std::shared_ptr<Promise<void>> JHybridNitroFetchClientSpec::prefetch(const NitroRequest& req) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JNitroRequest> /* req */)>("prefetch");
    auto __result = method(_javaPart, JNitroRequest::fromCpp(req));
    return [&]() {
      auto __promise = Promise<void>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& /* unit */) {
        __promise->resolve();
      });
      __result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JThrowable>& __throwable) {
        jni::JniException __jniError(__throwable);
        __promise->reject(std::make_exception_ptr(__jniError));
      });
      return __promise;
    }();
  }
  NitroResponse JHybridNitroFetchClientSpec::requestSync(const NitroRequest& req) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<jni::local_ref<JNitroResponse>(jni::alias_ref<JNitroRequest> /* req */)>("requestSync");
    auto __result = method(_javaPart, JNitroRequest::fromCpp(req));
    return __result->toCpp();
  }
  void JHybridNitroFetchClientSpec::cancelRequest(const std::string& requestId) {
    static const auto method = _javaPart->javaClassStatic()->getMethod<void(jni::alias_ref<jni::JString> /* requestId */)>("cancelRequest");
    method(_javaPart, jni::make_jstring(requestId));
  }

} // namespace margelo::nitro::nitrofetch
