namespace SharepointToolbox.Web.Core.Models;
///
/// Editable node for the visual folder-structure builder. SharePoint folders
/// are limited to 4 nesting levels here, matching the CSV template (Level1..Level4).
///
public class FolderNode
{
public const int MaxDepth = 4;
public string Name { get; set; } = string.Empty;
public List Children { get; } = new();
/// Flatten the tree into one per leaf path.
public static List Flatten(IEnumerable roots)
{
var rows = new List();
foreach (var root in roots)
Walk(root, new List(), rows);
return rows;
}
private static void Walk(FolderNode node, List ancestors, List rows)
{
var name = node.Name.Trim();
if (string.IsNullOrEmpty(name)) return;
var path = new List(ancestors) { name };
// Only emit a row for leaves; intermediate folders are created as ancestors of the leaf path.
if (node.Children.Count == 0)
{
rows.Add(new FolderStructureRow
{
Level1 = path.ElementAtOrDefault(0) ?? string.Empty,
Level2 = path.ElementAtOrDefault(1) ?? string.Empty,
Level3 = path.ElementAtOrDefault(2) ?? string.Empty,
Level4 = path.ElementAtOrDefault(3) ?? string.Empty,
});
return;
}
if (path.Count >= MaxDepth) return; // can't nest deeper
foreach (var child in node.Children)
Walk(child, path, rows);
}
}