# Hebrew Local Transcriber WebAssembly module. Copied into sherpa-onnx's wasm/hebrew/ by
# scripts/build_sherpa_wasm.py, which also adds it to wasm/CMakeLists.txt. TTS is not built.
if(NOT SHERPA_ONNX_ENABLE_WASM)
  message(FATAL_ERROR "This module is for WebAssembly.")
endif()

if(SHERPA_ONNX_ENABLE_TTS)
  message(FATAL_ERROR "Build with -DSHERPA_ONNX_ENABLE_TTS=OFF: this module must not contain TTS code.")
endif()

# Only what hlt_wasm.cc defines, plus the allocator JavaScript uses to pass samples in.
set(exported_functions
  hlt_build_info hlt_free hlt_free_string
  hlt_asr_create hlt_asr_destroy hlt_asr_decode
  hlt_asr_stream_create hlt_asr_stream_destroy hlt_asr_stream_feed
  hlt_asr_stream_is_endpoint hlt_asr_stream_reset
  hlt_vad_create hlt_vad_destroy hlt_vad_accept hlt_vad_flush hlt_vad_next
  hlt_vad_segment_start hlt_vad_segment_samples hlt_vad_pop
  hlt_diar_create hlt_diar_destroy hlt_diar_process
)
list(TRANSFORM exported_functions PREPEND "_" OUTPUT_VARIABLE mangled)
list(APPEND mangled _malloc _free)
list(JOIN mangled "," all_exported_functions)

add_executable(hlt-sherpa hlt_wasm.cc)
target_include_directories(hlt-sherpa PRIVATE ${CMAKE_SOURCE_DIR})
target_link_libraries(hlt-sherpa sherpa-onnx-c-api)

# Runs in a Web Worker (and in Node for tests). Memory starts small and grows: mobile browsers
# refuse large up-front reservations. Models are written into the in-memory file system by the
# worker, handed to sherpa by path, and deleted again once the sessions exist.
target_link_options(hlt-sherpa PRIVATE
  "SHELL:-sMODULARIZE=1"
  "SHELL:-sEXPORT_NAME=HebrewLocalTranscriberWasm"
  "SHELL:-sENVIRONMENT=web,worker,node"
  "SHELL:-sFORCE_FILESYSTEM=1"
  "SHELL:-sINITIAL_MEMORY=64MB"
  "SHELL:-sALLOW_MEMORY_GROWTH=1"
  "SHELL:-sMAXIMUM_MEMORY=2GB"
  "SHELL:-sSTACK_SIZE=5242880"
  "SHELL:-sALLOW_TABLE_GROWTH=1"
  "SHELL:-sEXPORTED_FUNCTIONS=${all_exported_functions}"
  "SHELL:-sEXPORTED_RUNTIME_METHODS=['FS','HEAPU8','HEAPF32','HEAP32','UTF8ToString','stringToUTF8','lengthBytesUTF8','addFunction','removeFunction']"
)

install(TARGETS hlt-sherpa DESTINATION bin/wasm/hebrew)
install(FILES
  "$<TARGET_FILE_DIR:hlt-sherpa>/hlt-sherpa.js"
  "$<TARGET_FILE_DIR:hlt-sherpa>/hlt-sherpa.wasm"
  DESTINATION bin/wasm/hebrew
)
