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

#include "rte_config.h"

#include <node_api.h>

#include "lib/shared_ptr.h"
#include "rte_runtime/binding/nodejs/src/common/common.h"
#include "rte_runtime/binding/nodejs/src/common/tsfn.h"
#include "rte_runtime/extension_group/extension_group.h"
#include "rte_runtime/sanitizer/thread_check.h"

#define RTE_NODEJS_EXTENSION_GROUP_SIGNATURE 0x371942FD8764236AU

typedef struct rte_nodejs_sem_t rte_nodejs_sem_t;

typedef struct rte_nodejs_extension_group_t {
  rte_signature_t signature;

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

  // Point to the corresponding C extension group.
  rte_extension_group_t* c_extension_group;
  napi_ref js_ref;      // Point to the corresponding JS extension group.
  napi_ref js_rte_ref;  // companion RTE of the JS extension group

  // @{
  // The following function is the Javascript functions corresponding to the
  // extension-group's interface API.
  rte_nodejs_threadsafe_function_t* js_on_init;
  rte_nodejs_threadsafe_function_t* js_on_deinit;
  rte_nodejs_threadsafe_function_t* js_on_create_extension;
  rte_nodejs_threadsafe_function_t* js_on_delete_extension;
  // @}

  rte_sanitizer_thread_check_t thread_check;
} rte_nodejs_extension_group_t;

AGORA_RTE_PRIVATE_API bool rte_nodejs_extension_group_check_integrity(
    rte_nodejs_extension_group_t* self, bool check_thread);

AGORA_RTE_API napi_value
rte_nodejs_extension_group_module_init(napi_env env, napi_value exports);
