/**
 *
 * Agora Real Time Engagement
 * Created by Wei Hu in 2021-11.
 * Copyright (c) 2022 Agora IO. All rights reserved.
 *
 */
#pragma once

#include "rte_config.h"

#include <assert.h>
#include <node_api.h>

#include "lib/signature.h"
#include "rte_runtime/binding/nodejs/src/common/tsfn.h"
#include "rte_runtime/dynamic/addon/addon.h"
#include "rte_runtime/sanitizer/thread_check.h"

#define RTE_NODEJS_ADDON_SIGNATURE 0xAB6B372F015D9CE9U

typedef struct rte_nodejs_addon_t {
  rte_signature_t signature;

  rte_string_t addon_name;
  int64_t is_extension;

  // Used to determine the timing of destroying this addon bridge.
  rte_sharedptr_t *shared_ptr;

  // Point to the corresponding C addon structure.
  rte_dynamic_factory_t c_addon;

  // Point to the corresponding JS addon.
  napi_ref js_ref;

  // @{
  // The following functions represent the corresponded JavaScript functions
  // to the one defined in the above 'c_addon'.
  rte_nodejs_threadsafe_function_t *js_on_init;
  rte_nodejs_threadsafe_function_t *js_on_create_instance;
  rte_nodejs_threadsafe_function_t *js_on_destroy_instance;
  // @}

  bool on_init_done_is_called;

  rte_sanitizer_thread_check_t thread_check;
} rte_nodejs_addon_t;

AGORA_RTE_API napi_value rte_nodejs_addon_module_init(napi_env env,
                                                      napi_value exports);
