/**
 * Copyright 2022-2025 Theai, Inc. dba Inworld AI
 */

#include "Primitives/STT/InworldSTTTypes.h"

#include "InworldRuntimeSettings.h"
#include "InworldRuntimeUtils.h"

THIRD_PARTY_INCLUDES_START
#include "InworldRuntimeLibrary/inworld.h"
THIRD_PARTY_INCLUDES_END

DEFINE_LOG_CATEGORY(LogInworldSTT);

namespace inworld
{

void Convert(const FInworldSpeechRecognitionConfig& In, inworld::SpeechRecognitionConfig& Out)
{
	if (In.bHasLanguage)
	{
		inworld::Language language;
		inworld::Convert(In.Language, language);
		Out.language()->set_value(language);
	}
}

void Convert(const FInworldLocalSTTCreationConfig& In, inworld::LocalSTTConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	inworld::Device device{FInworldRuntimeUtils::GetDevice(
		(In.LocalComputeHost == EInworldLocalComputeHost::CPU) ? inworld::DeviceType::kCPU : inworld::DeviceType::kCUDA)};
	Out.device(&device);
	Out.model_path({TCHAR_TO_UTF8(*GetInworldExtrasPath("stt", In.Model))});
	inworld::Convert(In.SpeechRecognitionConfig, *Out.default_config());
#endif
}

void Convert(const FInworldRemoteSTTCreationConfig& In, inworld::RemoteSTTConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	Out.api_key({TCHAR_TO_UTF8(*GetInworldRuntimeApiKey())});
	inworld::Convert(In.SpeechRecognitionConfig, *Out.default_config());
#endif
}

void Convert(const FInworldStreamSpeechRecognitionConfig& In, inworld::StreamSpeechRecognitionConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	inworld::Convert(In.SpeechRecognitionConfig, *Out.speech_config());
	Out.silence_threshold_ms(In.SilenceThresholdMs);
#endif
}

void Convert(const FInworldLocalStreamingSTTCreationConfig& In, inworld::LocalStreamingSTTConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	inworld::Device device{FInworldRuntimeUtils::GetDevice(
		(In.LocalComputeHost == EInworldLocalComputeHost::CPU) ? inworld::DeviceType::kCPU : inworld::DeviceType::kCUDA)};
	Out.stt_device(&device);
	Out.stt_model_path({TCHAR_TO_UTF8(*GetInworldExtrasPath("stt", In.Model))});
	Out.vad_model_path({TCHAR_TO_UTF8(*GetInworldExtrasPath("vad", "silero", "silero_vad.onnx"))});
	inworld::Convert(In.StreamSpeechRecognitionConfig, *Out.default_config());
#endif
}

void Convert(const FInworldRemoteStreamingSTTCreationConfig& In, inworld::RemoteStreamingSTTConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	Out.api_key({TCHAR_TO_UTF8(*GetInworldRuntimeApiKey())});
	inworld::Convert(In.StreamSpeechRecognitionConfig, *Out.default_config());
#endif
}

void Convert(const FInworldSTTNodeExecutionConfig& In, inworld::STTNodeExecutionConfig& Out)
{
#if defined(INWORLD_IMPLEMENTATION)
	if (In.bStreamed)
	{
		Out.stt_component_id({TCHAR_TO_UTF8(*FString("StreamingSTT_").Append(In.StreamingSTTComponentId))});
	}
	else
	{
		Out.stt_component_id({TCHAR_TO_UTF8(*FString("STT_").Append(In.STTComponentId))});
	}
#endif
}

}	 // namespace inworld
