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

#include "Graph/Nodes/InworldNode_TextChunking.h"

#include "Async/Async.h"
#include "Dom/JsonObject.h"
#include "Graph/InworldGraph.h"
#include "Misc/TVariant.h"
#include "Serialization/JsonSerializer.h"

UInworldNode_TextChunking::UInworldNode_TextChunking()
{
	NodeName = "Text Chunking";
}

EInworldRuntimeErrors UInworldNode_TextChunking::GetJsonConfig(const TSharedRef<FJsonObject>& GraphJson)
{
	TSharedPtr<FJsonObject> NodeObj = MakeShared<FJsonObject>();
	NodeObj->SetStringField(TEXT("id"), GetNodeName());
	NodeObj->SetStringField(TEXT("type"), TEXT("TextChunkingNode"));

	// execution_config
	{
		TSharedPtr<FJsonObject> ExecCfg = MakeShared<FJsonObject>();
		TSharedPtr<FJsonObject> ExecProps = MakeShared<FJsonObject>();
		ExecProps->SetNumberField(TEXT("min_chunk_length"), 80);
		ExecCfg->SetObjectField(TEXT("properties"), ExecProps);

		ExecCfg->SetStringField(TEXT("type"), TEXT("TextChunkingNodeExecutionConfig"));
		NodeObj->SetObjectField(TEXT("execution_config"), ExecCfg);
	}

	TSharedPtr<FJsonObject> Main = GraphJson->GetObjectField(TEXT("main"));
	TArray<TSharedPtr<FJsonValue>> Nodes = Main->GetArrayField(TEXT("nodes"));
	Nodes.Add(MakeShared<FJsonValueObject>(NodeObj));
	Main->SetArrayField(TEXT("nodes"), Nodes);

	return EInworldRuntimeErrors::SUCCESS;
}
