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

#define RTE_NODEJS_APP_SIGNATURE 0x133066C5116F5EA4U

typedef struct rte_nodejs_sem_t rte_nodejs_sem_t;

typedef struct rte_nodejs_app_t {
  rte_signature_t signature;

  bool is_default;

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

  rte_app_t* c_app;     // Point to the corresponding C app.
  napi_ref js_ref;      // Point to the corresponding JS app.
  napi_ref js_rte_ref;  // companion RTE of the JS app

  // @{
  // The following function is the Javascript functions corresponding to the
  // app's interface API.
  rte_nodejs_threadsafe_function_t* js_on_init;
  rte_nodejs_threadsafe_function_t* js_on_close;
  // @}

  rte_sanitizer_thread_check_t thread_check;
} rte_nodejs_app_t;

AGORA_RTE_API napi_value rte_nodejs_app_module_init(napi_env env,
                                                    napi_value exports);
