#include "UIStyleTokens.h"

#include "UObject/Class.h"
#include "UObject/UnrealType.h"

void FUIStylePanel::PostSerialize(const FArchive& Ar)
{
	// Migração v1→v2: TMap<EUIPanelTexture, ...> (legacy) → TMap<FName, ...> (atual).
	// Roda no PostLoad de cada FUIStyleRow; depois do primeiro re-save no editor
	// os campos *_DEPRECATED ficam vazios e este código vira no-op.
	if (!Ar.IsLoading())
	{
		return;
	}

	const UEnum* EnumPtr = StaticEnum<EUIPanelTexture>();
	if (EnumPtr == nullptr)
	{
		return;
	}

	auto MigrateOne = [EnumPtr](
		TMap<EUIPanelTexture, FSlateBrush>& Legacy,
		TMap<FName, FSlateBrush>& ByName)
	{
		if (Legacy.IsEmpty())
		{
			return;
		}
		for (const TPair<EUIPanelTexture, FSlateBrush>& Pair : Legacy)
		{
			if (Pair.Key == EUIPanelTexture::None)
			{
				continue;
			}
			const FName KeyName(*EnumPtr->GetAuthoredNameStringByValue(static_cast<int64>(Pair.Key)));
			if (!ByName.Contains(KeyName))
			{
				ByName.Add(KeyName, Pair.Value);
			}
		}
		Legacy.Reset();
	};

	MigrateOne(PanelBackground_DEPRECATED,    PanelBackgroundByName);
	MigrateOne(PanelOutline_DEPRECATED,       PanelOutlineByName);
	MigrateOne(PanelOutlineEffect_DEPRECATED, PanelOutlineEffectByName);
}
