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

#include "JHybridFetchSpec.hpp"

// Forward declaration of `NativeResponse` to properly resolve imports.
namespace margelo::nitro::realfetch { struct NativeResponse; }
// Forward declaration of `HybridInputStreamSpec` to properly resolve imports.
namespace margelo::nitro::realfetch { class HybridInputStreamSpec; }
// Forward declaration of `NativeRequest` to properly resolve imports.
namespace margelo::nitro::realfetch { struct NativeRequest; }
// Forward declaration of `RequestMethods` to properly resolve imports.
namespace margelo::nitro::realfetch { enum class RequestMethods; }
// Forward declaration of `RequestRedirection` to properly resolve imports.
namespace margelo::nitro::realfetch { enum class RequestRedirection; }

#include "NativeResponse.hpp"
#include <NitroModules/Promise.hpp>
#include <NitroModules/JPromise.hpp>
#include "JNativeResponse.hpp"
#include <string>
#include <vector>
#include <memory>
#include "HybridInputStreamSpec.hpp"
#include <optional>
#include "JHybridInputStreamSpec.hpp"
#include "NativeRequest.hpp"
#include "JNativeRequest.hpp"
#include "RequestMethods.hpp"
#include "JRequestMethods.hpp"
#include "RequestRedirection.hpp"
#include "JRequestRedirection.hpp"

namespace margelo::nitro::realfetch {

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

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

  size_t JHybridFetchSpec::getExternalMemorySize() noexcept {
    static const auto method = javaClassStatic()->getMethod<jlong()>("getMemorySize");
    return method(_javaPart);
  }

  void JHybridFetchSpec::dispose() noexcept {
    static const auto method = javaClassStatic()->getMethod<void()>("dispose");
    method(_javaPart);
  }

  // Properties
  

  // Methods
  std::shared_ptr<Promise<NativeResponse>> JHybridFetchSpec::create(const NativeRequest& opts) {
    static const auto method = javaClassStatic()->getMethod<jni::local_ref<JPromise::javaobject>(jni::alias_ref<JNativeRequest> /* opts */)>("create");
    auto __result = method(_javaPart, JNativeRequest::fromCpp(opts));
    return [&]() {
      auto __promise = Promise<NativeResponse>::create();
      __result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& __boxedResult) {
        auto __result = jni::static_ref_cast<JNativeResponse>(__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;
    }();
  }

} // namespace margelo::nitro::realfetch
