namespace Inspectron.Epson.Templates.Language.Nodes;

public record ForeachNode(
    string ItemVariable,
    string CollectionPath,
    List<ITemplateNode> Body,
    SourcePosition Position) : ITemplateNode
{
    public IReadOnlyList<ITemplateNode> Children => Body;

    public static (string ItemVariable, string CollectionPath) ParseForeach(string value)
    {
        // Expected format: "item in collection" or "item in collection.path"
        var parts = value.Split(" in ", 2, StringSplitOptions.TrimEntries);
        if (parts.Length != 2)
        {
            throw new FormatException($"Invalid foreach syntax: '{value}'. Expected 'item in collection'.");
        }
        return (parts[0], parts[1]);
    }
}
