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

#include "K2Node_InworldCreateStructJinjaArgument.h"

#include "BlueprintActionDatabaseRegistrar.h"
#include "BlueprintNodeSpawner.h"
#include "EdGraphSchema_K2.h"
#include "InworldBlueprintFunctionLibrary.h"

void UK2Node_InworldCreateStructJinjaArgument::GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const
{
	Super::GetMenuActions(ActionRegistrar);
	UClass* Action = GetClass();
	if (ActionRegistrar.IsOpenForRegistration(Action))
	{
		auto CustomizeLambda = [](UEdGraphNode* NewNode, bool bIsTemplateNode, const FName FunctionName)
		{
			UK2Node_InworldCreateStructJinjaArgument* Node = CastChecked<UK2Node_InworldCreateStructJinjaArgument>(NewNode);
			UFunction* Function = UInworldBlueprintFunctionLibrary::StaticClass()->FindFunctionByName(FunctionName);
			check(Function);
			Node->SetFromFunction(Function);
		};

		UBlueprintNodeSpawner* GetNodeSpawner = UBlueprintNodeSpawner::Create(GetClass());
		check(GetNodeSpawner != nullptr);
		GetNodeSpawner->CustomizeNodeDelegate = UBlueprintNodeSpawner::FCustomizeNodeDelegate::CreateStatic(
			CustomizeLambda, GET_FUNCTION_NAME_CHECKED(UInworldBlueprintFunctionLibrary, CreateStructJinjaArgument));
		ActionRegistrar.AddBlueprintAction(Action, GetNodeSpawner);
	}
}

bool UK2Node_InworldCreateStructJinjaArgument::IsConnectionDisallowed(
	const UEdGraphPin* MyPin, const UEdGraphPin* OtherPin, FString& OutReason) const
{
	if (MyPin == FindPinChecked(FName{"Data"}))
	{
		if (UScriptStruct* PinStruct = Cast<UScriptStruct>(OtherPin->PinType.PinSubCategoryObject.Get());
			OtherPin->PinType.PinCategory != UEdGraphSchema_K2::PC_Struct || PinStruct == nullptr)
		{
			OutReason = TEXT("Pin type must be struct type.");
			return true;
		}
	}
	return Super::IsConnectionDisallowed(MyPin, OtherPin, OutReason);
}
