#include "Utils.h"

#include <Sync.h>

namespace sharedjsi
{
Function dittoffi_ditto_is_activated(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
                                          {
    return ::dittoffi_ditto_is_activated(jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]));
  });
}

Function dittoffi_ditto_is_sync_active(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
                                          {
    return ::dittoffi_ditto_is_sync_active(jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]));
  });
}

Function dittoffi_ditto_try_start_sync(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
                                          {
    dittoffi_result_void_t res = ::dittoffi_ditto_try_start_sync(jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]));

    Object obj(runtime);
    obj.setProperty(runtime, "error", cPointerToJSPointerObject(runtime, res.error));

    return obj;
  });
}

Function dittoffi_ditto_stop_sync(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
                                          {
    ::dittoffi_ditto_stop_sync(jsPointerObjectToCPointer<CDitto_t>(runtime, arguments[0]));
    return {};
  });
}
}
