#include "HyperTwistTraining/HyperTwistTrainingTilingLibrary.h"

namespace HyperTwistTrainingTilingLibraryInternal
{
	const TCHAR* MagicTileLicense = TEXT("MIT");

	FHyperTwistTrainingSourceAttribution MakeAttribution(
		const FString& TrackId,
		const FString& Notes
	)
	{
		FHyperTwistTrainingSourceAttribution Attribution;
		Attribution.SourceMode = EHyperTwistTrainingSourceMode::DirectDonor;
		Attribution.SourceRepo = TEXT("roice3/MagicTile");
		Attribution.SourceLicense = MagicTileLicense;
		Attribution.SourceTrackId = TrackId;
		Attribution.SourceNotes = Notes;
		return Attribution;
	}

	FHyperTwistTrainingTilingPackageReference MakePackageReference(
		const FString& PackageId,
		const FString& Label,
		const FString& ExportPath,
		const FString& OwnershipRole,
		const FString& LicensePosture,
		const FString& Summary,
		const TArray<FString>& RetainedSurfaceTags,
		const TArray<FString>& RequiredNoticeActions,
		const TArray<FString>& ExplicitExclusions,
		const FHyperTwistTrainingSourceAttribution& SourceAttribution
	)
	{
		FHyperTwistTrainingTilingPackageReference Reference;
		Reference.PackageId = PackageId;
		Reference.Label = Label;
		Reference.ExportPath = ExportPath;
		Reference.OwnershipRole = OwnershipRole;
		Reference.LicensePosture = LicensePosture;
		Reference.Summary = Summary;
		Reference.RetainedSurfaceTags = RetainedSurfaceTags;
		Reference.RequiredNoticeActions = RequiredNoticeActions;
		Reference.ExplicitExclusions = ExplicitExclusions;
		Reference.SourceAttribution = SourceAttribution;
		return Reference;
	}

	void AppendJoined(FString& OutText, const TArray<FString>& Values)
	{
		for (int32 Index = 0; Index < Values.Num(); ++Index)
		{
			if (Index > 0)
			{
				OutText.Append(TEXT(", "));
			}

			OutText.Append(Values[Index]);
		}
	}
}

FHyperTwistTrainingTilingReferenceBundle UHyperTwistTrainingTilingLibrary::BuildBundledTilingReferenceBundle()
{
	using namespace HyperTwistTrainingTilingLibraryInternal;

	FHyperTwistTrainingTilingReferenceBundle Bundle;
	Bundle.ReferenceUtc = TEXT("2026-05-24T18:30:00Z");
	Bundle.PrimaryTopologyContractId = TEXT("tiling-topology-stack");
	Bundle.PrimaryLoaderBoundaryId = TEXT("tiling-loader-boundary");
	Bundle.PrimaryAttributionBoundaryId = TEXT("tiling-mit-attribution-boundary");
	Bundle.PrimaryMacroRemappingContractId = TEXT("tiling-macro-remapping-stack");
	Bundle.PrimaryMacroPersistenceBoundaryId = TEXT("tiling-macro-persistence-boundary");

	const TArray<FString> CommonNoticeActions =
	{
		TEXT("preserve the upstream MIT license text in third-party notices or equivalent release/legal materials"),
		TEXT("preserve the upstream copyright notice for Roice Nelson")
	};

	Bundle.PackageReferences =
	{
		MakePackageReference(
			TEXT("package/magictile-topology-analyzer"),
			TEXT("MagicTile/Puzzle/TopologyAnalyzer"),
			TEXT("./MagicTile/Puzzle/TopologyAnalyzer.cs"),
			TEXT("tiling-topology-anchor"),
			TEXT("direct permissive donor"),
			TEXT("Logical face-edge-vertex identification, Euler-characteristic calculation, and merged topology analysis for tiling-driven puzzle spaces."),
			{
				TEXT("logical element indexing"),
				TEXT("face edge vertex counts"),
				TEXT("Euler characteristic"),
				TEXT("master/slave identification merging")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("WinForms/OpenTK shell ownership"),
				TEXT("macro transport ownership")
			},
			MakeAttribution(
				TEXT("tiling/topology-analyzer"),
				TEXT("Preserves MagicTile logical-element indexing, identification merging, and Euler-characteristic analysis beneath first-party HyperTwist tiling topology ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-loader"),
			TEXT("MagicTile/Puzzle/Loader"),
			TEXT("./MagicTile/Puzzle/Loader.cs"),
			TEXT("tiling-loader-support"),
			TEXT("direct permissive donor"),
			TEXT("Generalized puzzle-construction loading, master/slave cell preparation, and topology-derived construction support for tiling-driven puzzle families."),
			{
				TEXT("puzzle loading"),
				TEXT("master slave cell setup"),
				TEXT("state-calculation cell preparation"),
				TEXT("topology-derived construction")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("whole legacy persistence shell"),
				TEXT("direct WinForms host transplant")
			},
			MakeAttribution(
				TEXT("tiling/loader"),
				TEXT("Preserves MagicTile generalized puzzle-loading and topology-derived construction seams without displacing the Hyperspeedcube runtime anchor.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-puzzle-core"),
			TEXT("MagicTile/Puzzle/Puzzle"),
			TEXT("./MagicTile/Puzzle/Puzzle.cs"),
			TEXT("tiling-construction-support"),
			TEXT("direct permissive donor"),
			TEXT("Tiling-driven puzzle construction support including master/slave cells, sticker slicing, and twist-data registration beneath first-party runtime ownership."),
			{
				TEXT("master cells"),
				TEXT("slave cells"),
				TEXT("sticker slicing"),
				TEXT("twist-data registration")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("whole renderer ownership"),
				TEXT("legacy host-shell ownership")
			},
			MakeAttribution(
				TEXT("tiling/puzzle-core"),
				TEXT("Preserves MagicTile master/slave construction, sticker slicing, and twist-data setup seams beneath first-party HyperTwist topology ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-tiling-geometry"),
			TEXT("R3.Core/Geometry/Tiling"),
			TEXT("./R3/R3.Core/Geometry/Tiling.cs"),
			TEXT("tiling-geometry-anchor"),
			TEXT("direct permissive donor"),
			TEXT("Regular-tiling, dual-tiling, and centered-projection geometry substrate for spherical, Euclidean, and hyperbolic tiling families."),
			{
				TEXT("regular tiling generation"),
				TEXT("dual tiling support"),
				TEXT("face edge vertex centered projections"),
				TEXT("non-Euclidean tiling families")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("macro remapping ownership"),
				TEXT("legacy renderer shell")
			},
			MakeAttribution(
				TEXT("tiling/geometry-tiling"),
				TEXT("Preserves MagicTile regular-tiling, dual-tiling, and projection-family seams for first-party HyperTwist tiling geometry ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-polytope-geometry"),
			TEXT("R3.Core/Geometry/Polytope"),
			TEXT("./R3/R3.Core/Geometry/Polytope.cs"),
			TEXT("tiling-polytope-support"),
			TEXT("direct permissive donor"),
			TEXT("Polytope construction support for topology-derived puzzle families that widen beyond ordinary cube spaces."),
			{
				TEXT("polytope construction"),
				TEXT("cell and facet relationships"),
				TEXT("higher-order geometry support")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("whole viewer shell ownership"),
				TEXT("direct renderer transplant")
			},
			MakeAttribution(
				TEXT("tiling/geometry-polytope"),
				TEXT("Preserves MagicTile polytope-construction support for first-party HyperTwist topology and geometry-family ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-h3-honeycomb"),
			TEXT("R3.Core/Honeycombs/H3"),
			TEXT("./R3/R3.Core/Honeycombs/H3.cs"),
			TEXT("tiling-hyperbolic-family-support"),
			TEXT("direct permissive donor"),
			TEXT("Hyperbolic honeycomb-family support for later non-Euclidean tiling puzzle families beneath first-party HyperTwist ownership."),
			{
				TEXT("hyperbolic honeycomb families"),
				TEXT("non-Euclidean family tags"),
				TEXT("later topology widening support")
			},
			CommonNoticeActions,
			{
				TEXT("runtime-base ownership"),
				TEXT("whole app-shell ownership"),
				TEXT("macro remapping ownership")
			},
			MakeAttribution(
				TEXT("tiling/h3-honeycomb"),
				TEXT("Preserves MagicTile hyperbolic honeycomb-family seams for later first-party HyperTwist tiling and non-Euclidean topology widening.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-macro"),
			TEXT("MagicTile/Puzzle/Twisting/Macro"),
			TEXT("./MagicTile/Puzzle/Twisting/Macro.cs"),
			TEXT("tiling-macro-remapping-anchor"),
			TEXT("direct permissive donor"),
			TEXT("Reference Mobius orientation, transformed twist lookup, reverse or reflection handling, and macro XML persistence for topology-aware advanced puzzle macros."),
			{
				TEXT("reference mobius transform"),
				TEXT("clicked-cell canonical orientation"),
				TEXT("view reflection parity"),
				TEXT("transformed twist lookup"),
				TEXT("macro XML persistence")
			},
			CommonNoticeActions,
			{
				TEXT("whole macro editor shell"),
				TEXT("broad non-Euclidean interaction shell ownership"),
				TEXT("WinForms/OpenTK host-shell ownership")
			},
			MakeAttribution(
				TEXT("tiling/macro"),
				TEXT("Preserves MagicTile reference Mobius setup, transformed twist lookup, reversal handling, and XML macro persistence beneath first-party HyperTwist macro-remapping ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-twist-handler"),
			TEXT("MagicTile/Puzzle/Twisting/TwistHandler"),
			TEXT("./MagicTile/Puzzle/Twisting/TwistHandler.cs"),
			TEXT("tiling-macro-playback-support"),
			TEXT("direct permissive donor"),
			TEXT("Macro playback, reverse execution, setup-move unwind, commutator playback, and grouped undo or redo handling for advanced tiling puzzles."),
			{
				TEXT("forward macro execution"),
				TEXT("reverse macro execution"),
				TEXT("setup-move unwind"),
				TEXT("commutator playback"),
				TEXT("grouped macro undo redo")
			},
			CommonNoticeActions,
			{
				TEXT("whole click-input shell"),
				TEXT("animation timing shell ownership"),
				TEXT("legacy status or menu ownership")
			},
			MakeAttribution(
				TEXT("tiling/twist-handler"),
				TEXT("Preserves MagicTile macro playback, reversal, setup-move, and grouped undo behavior without promoting the old desktop interaction shell into first-party HyperTwist ownership.")
			)
		),
		MakePackageReference(
			TEXT("package/magictile-setup-moves"),
			TEXT("MagicTile/Puzzle/Twisting/SetupMoves"),
			TEXT("./MagicTile/Puzzle/Twisting/SetupMoves.cs"),
			TEXT("tiling-setup-move-support"),
			TEXT("direct permissive donor"),
			TEXT("Setup and commutator move recording that complements transform-aware macro remapping without owning the broader puzzle shell."),
			{
				TEXT("setup move recording"),
				TEXT("commutator derivation"),
				TEXT("macro reversal reuse")
			},
			CommonNoticeActions,
			{
				TEXT("whole macro-pane shell"),
				TEXT("legacy accelerator ownership"),
				TEXT("runtime-base ownership")
			},
			MakeAttribution(
				TEXT("tiling/setup-moves"),
				TEXT("Preserves MagicTile setup-move and commutator recording seams beneath first-party HyperTwist macro-remapping ownership.")
			)
		)
	};

	FHyperTwistTrainingTilingTopologyContract TopologyContract;
	TopologyContract.ContractId = TEXT("tiling-topology-stack");
	TopologyContract.Title = TEXT("Tiling topology and geometry-family contract");
	TopologyContract.Summary = TEXT("First-party HyperTwist contract for tiling-driven puzzle-family descriptors, logical topology analysis, and non-Euclidean geometry-family tags beneath the Hyperspeedcube runtime anchor.");
	TopologyContract.RequiredPackageIds =
	{
		TEXT("package/magictile-topology-analyzer"),
		TEXT("package/magictile-tiling-geometry"),
		TEXT("package/magictile-polytope-geometry"),
		TEXT("package/magictile-h3-honeycomb")
	};
	TopologyContract.PuzzleFamilies =
	{
		TEXT("spherical tilings"),
		TEXT("Euclidean tilings"),
		TEXT("hyperbolic tilings"),
		TEXT("tiling-driven non-Euclidean puzzle families")
	};
	TopologyContract.GeometrySurfaceTags =
	{
		TEXT("regular tiling generation"),
		TEXT("dual-tiling support"),
		TEXT("polytope geometry support"),
		TEXT("hyperbolic honeycomb family tags"),
		TEXT("face edge vertex centered projections")
	};
	TopologyContract.TopologySurfaceTags =
	{
		TEXT("logical face edge vertex counts"),
		TEXT("Euler characteristic"),
		TEXT("master slave identification merging"),
		TEXT("topology-derived indexing"),
		TEXT("state-calculation cell topology")
	};
	TopologyContract.ExplicitExclusions =
	{
		TEXT("Hyperspeedcube runtime-base replacement"),
		TEXT("WinForms/OpenTK shell ownership"),
		TEXT("transform-aware macro remapping ownership")
	};
	TopologyContract.SourceAttribution = MakeAttribution(
		TEXT("tiling/topology-contract"),
		TEXT("Preserves MagicTile tiling-family descriptors, topology analysis, and non-Euclidean geometry-family seams while keeping HyperTwist's runtime anchor and product shell first-party.")
	);
	Bundle.TopologyContracts = {TopologyContract};

	FHyperTwistTrainingTilingLoaderBoundary LoaderBoundary;
	LoaderBoundary.BoundaryId = TEXT("tiling-loader-boundary");
	LoaderBoundary.Title = TEXT("Tiling loader and runtime-anchor boundary");
	LoaderBoundary.Summary = TEXT("First-party HyperTwist boundary for generalized tiling-derived puzzle loading beneath the Hyperspeedcube runtime anchor and outside the legacy host shell.");
	LoaderBoundary.RequiredPackageIds =
	{
		TEXT("package/magictile-loader"),
		TEXT("package/magictile-puzzle-core"),
		TEXT("package/magictile-topology-analyzer")
	};
	LoaderBoundary.LoaderSurfaceTags =
	{
		TEXT("generalized puzzle loading"),
		TEXT("master slave cell preparation"),
		TEXT("sticker slicing"),
		TEXT("twist-data registration"),
		TEXT("topology-derived construction")
	};
	LoaderBoundary.RuntimeBoundaryTags =
	{
		TEXT("Hyperspeedcube runtime anchor remains primary for runtime catalog notation replay and verification"),
		TEXT("MagicTile widens only the tiling-derived topology and geometry-family lane"),
		TEXT("loader enrichments stay beneath first-party runtime ownership")
	};
	LoaderBoundary.ExplicitExclusions =
	{
		TEXT("Hyperspeedcube runtime-base replacement"),
		TEXT("whole WinForms/OpenTK shell ownership"),
		TEXT("legacy persistence-dialog ownership"),
		TEXT("transform-aware macro remapping ownership")
	};
	LoaderBoundary.SourceAttribution = MakeAttribution(
		TEXT("tiling/loader-boundary"),
		TEXT("Preserves MagicTile generalized puzzle-loading and topology-derived construction seams while keeping runtime, notation, replay, and verification ownership with the Hyperspeedcube anchor and first-party HyperTwist code.")
	);
	Bundle.LoaderBoundaries = {LoaderBoundary};

	FHyperTwistTrainingTilingMacroRemappingContract MacroContract;
	MacroContract.ContractId = TEXT("tiling-macro-remapping-stack");
	MacroContract.Title = TEXT("Tiling transform-aware macro remapping contract");
	MacroContract.Summary = TEXT("First-party HyperTwist contract for reference-cell macro orientation, transformed twist lookup, reflection-aware reversal, and bounded macro playback semantics for non-Euclidean tiling families.");
	MacroContract.RequiredPackageIds =
	{
		TEXT("package/magictile-macro"),
		TEXT("package/magictile-twist-handler"),
		TEXT("package/magictile-setup-moves"),
		TEXT("package/magictile-puzzle-core"),
		TEXT("package/magictile-topology-analyzer")
	};
	MacroContract.ReferenceSurfaceTags =
	{
		TEXT("reference mobius transform"),
		TEXT("clicked-cell canonical orientation"),
		TEXT("view reflection parity"),
		TEXT("puzzle-specific macro ownership")
	};
	MacroContract.TransformSurfaceTags =
	{
		TEXT("transformed twist lookup by closest center"),
		TEXT("state-calc twist selection nearest origin after transform"),
		TEXT("reverse XOR view-reflection handling"),
		TEXT("identified twist-data remapping")
	};
	MacroContract.PlaybackSurfaceTags =
	{
		TEXT("forward macro execution"),
		TEXT("reverse macro execution"),
		TEXT("setup-move unwind"),
		TEXT("commutator second-half playback"),
		TEXT("grouped macro undo redo")
	};
	MacroContract.ExplicitExclusions =
	{
		TEXT("whole macro editor shell"),
		TEXT("broad non-Euclidean interaction shell ownership"),
		TEXT("WinForms/OpenTK host-shell ownership"),
		TEXT("Hyperspeedcube runtime-base replacement")
	};
	MacroContract.SourceAttribution = MakeAttribution(
		TEXT("tiling/macro-remapping-contract"),
		TEXT("Preserves MagicTile macro reference orientation, transformed twist remapping, and reversal semantics while keeping HyperTwist first-party for the broader macro UX, runtime, and product shell.")
	);
	Bundle.MacroRemappingContracts = {MacroContract};

	FHyperTwistTrainingTilingMacroPersistenceBoundary MacroPersistenceBoundary;
	MacroPersistenceBoundary.BoundaryId = TEXT("tiling-macro-persistence-boundary");
	MacroPersistenceBoundary.Title = TEXT("Tiling macro persistence and compatibility boundary");
	MacroPersistenceBoundary.Summary = TEXT("First-party HyperTwist boundary for MagicTile-style macro XML persistence, puzzle compatibility checks, and macro block serialization beneath first-party macro ownership.");
	MacroPersistenceBoundary.RequiredPackageIds =
	{
		TEXT("package/magictile-macro"),
		TEXT("package/magictile-loader"),
		TEXT("package/magictile-puzzle-core")
	};
	MacroPersistenceBoundary.PersistenceSurfaceTags =
	{
		TEXT("MagicTileMacros XML root"),
		TEXT("macro display name persistence"),
		TEXT("Mobius persistence"),
		TEXT("view-reflected persistence"),
		TEXT("tab-delimited macro block serialization"),
		TEXT("macro start and end markers")
	};
	MacroPersistenceBoundary.CompatibilitySurfaceTags =
	{
		TEXT("active puzzle id compatibility"),
		TEXT("puzzle display-name compatibility messaging"),
		TEXT("edge or vertex twisting version compatibility"),
		TEXT("saved version 2.0 and 2.1 handling"),
		TEXT("load macros against all twist data")
	};
	MacroPersistenceBoundary.ProvenanceCaveats =
	{
		TEXT("macro files are puzzle-specific and are not intended to travel across differently configured puzzles"),
		TEXT("macro persistence uses XML blocks rather than the first-party canonical replay schema"),
		TEXT("save and load dialog filter strings remain legacy host-shell details rather than first-party product contract")
	};
	MacroPersistenceBoundary.ExplicitExclusions =
	{
		TEXT("generic replay or canonical notation ownership"),
		TEXT("host file-dialog ownership"),
		TEXT("whole macro-management pane shell"),
		TEXT("legacy message-box choreography")
	};
	MacroPersistenceBoundary.SourceAttribution = MakeAttribution(
		TEXT("tiling/macro-persistence-boundary"),
		TEXT("Preserves MagicTile macro XML persistence and compatibility checks while keeping HyperTwist first-party for canonical replay, notation, and broader persistence ownership.")
	);
	Bundle.MacroPersistenceBoundaries = {MacroPersistenceBoundary};

	FHyperTwistTrainingTilingAttributionBoundary AttributionBoundary;
	AttributionBoundary.BoundaryId = TEXT("tiling-mit-attribution-boundary");
	AttributionBoundary.Title = TEXT("Tiling donor attribution boundary");
	AttributionBoundary.Summary = TEXT("First-party HyperTwist compliance boundary for using MagicTile as a direct permissive donor for tiling topology, loader, and bounded macro-remapping contracts.");
	AttributionBoundary.AllowedUseTags =
	{
		TEXT("first-party reauthored subsystem implementation"),
		TEXT("direct permissive donor reference"),
		TEXT("bounded tiling topology, loader, and macro-remapping contract use")
	};
	AttributionBoundary.RequiredNoticeActions = CommonNoticeActions;
	AttributionBoundary.ExplicitExclusions =
	{
		TEXT("silent attribution removal"),
		TEXT("whole legacy app-shell adoption"),
		TEXT("pretending broad interaction-shell or runtime-base ownership landed in this packet")
	};
	AttributionBoundary.SourceAttribution = MakeAttribution(
		TEXT("tiling/attribution-boundary"),
		TEXT("Records MIT attribution and packet bounds for MagicTile while keeping non-owned shell surfaces excluded from the bounded tiling topology and macro-remapping slices.")
	);
	Bundle.AttributionBoundaries = {AttributionBoundary};

	return Bundle;
}

bool UHyperTwistTrainingTilingLibrary::TryGetTopologyContractById(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle,
	const FString& ContractId,
	FHyperTwistTrainingTilingTopologyContract& OutContract
)
{
	for (const FHyperTwistTrainingTilingTopologyContract& Contract : Bundle.TopologyContracts)
	{
		if (Contract.ContractId == ContractId)
		{
			OutContract = Contract;
			return true;
		}
	}

	OutContract = FHyperTwistTrainingTilingTopologyContract();
	return false;
}

bool UHyperTwistTrainingTilingLibrary::TryGetLoaderBoundaryById(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle,
	const FString& BoundaryId,
	FHyperTwistTrainingTilingLoaderBoundary& OutBoundary
)
{
	for (const FHyperTwistTrainingTilingLoaderBoundary& Boundary : Bundle.LoaderBoundaries)
	{
		if (Boundary.BoundaryId == BoundaryId)
		{
			OutBoundary = Boundary;
			return true;
		}
	}

	OutBoundary = FHyperTwistTrainingTilingLoaderBoundary();
	return false;
}

bool UHyperTwistTrainingTilingLibrary::TryGetAttributionBoundaryById(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle,
	const FString& BoundaryId,
	FHyperTwistTrainingTilingAttributionBoundary& OutBoundary
)
{
	for (const FHyperTwistTrainingTilingAttributionBoundary& Boundary : Bundle.AttributionBoundaries)
	{
		if (Boundary.BoundaryId == BoundaryId)
		{
			OutBoundary = Boundary;
			return true;
		}
	}

	OutBoundary = FHyperTwistTrainingTilingAttributionBoundary();
	return false;
}

bool UHyperTwistTrainingTilingLibrary::TryGetMacroRemappingContractById(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle,
	const FString& ContractId,
	FHyperTwistTrainingTilingMacroRemappingContract& OutContract
)
{
	for (const FHyperTwistTrainingTilingMacroRemappingContract& Contract : Bundle.MacroRemappingContracts)
	{
		if (Contract.ContractId == ContractId)
		{
			OutContract = Contract;
			return true;
		}
	}

	OutContract = FHyperTwistTrainingTilingMacroRemappingContract();
	return false;
}

bool UHyperTwistTrainingTilingLibrary::TryGetMacroPersistenceBoundaryById(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle,
	const FString& BoundaryId,
	FHyperTwistTrainingTilingMacroPersistenceBoundary& OutBoundary
)
{
	for (const FHyperTwistTrainingTilingMacroPersistenceBoundary& Boundary : Bundle.MacroPersistenceBoundaries)
	{
		if (Boundary.BoundaryId == BoundaryId)
		{
			OutBoundary = Boundary;
			return true;
		}
	}

	OutBoundary = FHyperTwistTrainingTilingMacroPersistenceBoundary();
	return false;
}

FString UHyperTwistTrainingTilingLibrary::BuildPackageChecklistTsv(
	const FHyperTwistTrainingTilingReferenceBundle& Bundle
)
{
	using namespace HyperTwistTrainingTilingLibraryInternal;

	FString Output = TEXT("packageId\tlabel\texportPath\townershipRole\tlicensePosture\tretainedSurfaceTags\texplicitExclusions\n");
	for (const FHyperTwistTrainingTilingPackageReference& Reference : Bundle.PackageReferences)
	{
		FString RetainedTags;
		AppendJoined(RetainedTags, Reference.RetainedSurfaceTags);

		FString ExplicitExclusions;
		AppendJoined(ExplicitExclusions, Reference.ExplicitExclusions);

		Output += FString::Printf(
			TEXT("%s\t%s\t%s\t%s\t%s\t%s\t%s\n"),
			*Reference.PackageId,
			*Reference.Label,
			*Reference.ExportPath,
			*Reference.OwnershipRole,
			*Reference.LicensePosture,
			*RetainedTags,
			*ExplicitExclusions
		);
	}

	return Output;
}
