/**
 *
 * 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/signature.h"
#include "rte_runtime/binding/nodejs/src/common/tsfn.h"
#include "rte_runtime/extension/extension.h"
#include "rte_runtime/sanitizer/thread_check.h"

#define RTE_NODEJS_EXTENSION_SIGNATURE 0xB7288D534BC053C3U

typedef struct rte_nodejs_sem_t rte_nodejs_sem_t;

typedef struct rte_nodejs_extension_t {
  rte_signature_t signature;

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

  rte_extension_t *c_extension;  // Point to the corresponding C extension.
  napi_ref js_ref;               // Point to the corresponding JS extension.
  napi_ref js_rte_ref;           // companion RTE of the JS extension

  // @{
  // The following function is the Javascript functions corresponding to the
  // extension's interface API.
  rte_nodejs_threadsafe_function_t *js_on_start;
  rte_nodejs_threadsafe_function_t *js_on_init;
  rte_nodejs_threadsafe_function_t *js_on_stop;
  rte_nodejs_threadsafe_function_t *js_on_cmd;
  rte_nodejs_threadsafe_function_t *js_on_data;
  rte_nodejs_threadsafe_function_t *js_on_pcm_frame;
  rte_nodejs_threadsafe_function_t *js_on_image_frame;
  // @}

  rte_sanitizer_thread_check_t thread_check;
} rte_nodejs_extension_t;

AGORA_RTE_PRIVATE_API bool rte_nodejs_extension_check_integrity(
    rte_nodejs_extension_t *self, bool check_thread);

AGORA_RTE_API napi_value rte_nodejs_extension_module_init(napi_env env,
                                                          napi_value exports);
