
#include "Utils.h"
#include "LiveQuery.h"

namespace sharedjsi
{

Function ditto_live_query_signal_available_next(Runtime &runtime)
{
  return Function::createFromHostFunction(runtime,
                                          PropNameID::forAscii(runtime,
                                                               STRINGIFIED_FUNC_NAME()),
                                          0,
                                          [](Runtime &runtime,
                                             const Value &thisValue,
                                             const Value *arguments,
                                             size_t count) -> Value
                                          {
    auto *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
    int64_t liveQueryId = bigIntOrNumberToInt64(runtime, arguments[1]);

    ::ditto_live_query_signal_available_next(ditto, liveQueryId);
    return {};
  });
}

Function ditto_live_query_start(Runtime &runtime)
{
  return Function::createFromHostFunction(runtime,
                                          PropNameID::forAscii(runtime,
                                                               STRINGIFIED_FUNC_NAME()),
                                          0,
                                          [](Runtime &runtime,
                                             const Value &thisValue,
                                             const Value *arguments,
                                             size_t count) -> Value
                                          {
    auto *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
    int64_t liveQueryId = bigIntOrNumberToInt64(runtime, arguments[1]);
    int32_t res = ::ditto_live_query_start(ditto, liveQueryId);
    return static_cast<double>(res);
  });
}

Function ditto_live_query_stop(Runtime &runtime)
{
  return Function::createFromHostFunction(runtime,
                                          PropNameID::forAscii(runtime,
                                                               STRINGIFIED_FUNC_NAME()),
                                          0,
                                          [](Runtime &runtime,
                                             const Value &thisValue,
                                             const Value *arguments,
                                             size_t count) -> Value
                                          {
    auto *ditto = jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]);
    int64_t liveQueryId = bigIntOrNumberToInt64(runtime, arguments[1]);

    ::ditto_live_query_stop(ditto, liveQueryId);
    return {};
  });
}
}
