unit Myc.Fmx.AstEditor.Handlers.Pipes;

interface

uses
    System.SysUtils,
    System.UITypes,
    System.Types,
    Myc.Ast,
    Myc.Ast.Nodes,
    Myc.Fmx.AstEditor.Render,
    Myc.Fmx.AstEditor.Node;

type
    // --- Pipe Container Handler ---
    // Repräsentiert die (pipe [[src [:sel]]...] (fn ...)) Struktur.
    TPipeNodeHandler = class(TBaseNodeHandler<IPipeNode>)
    private
        FInputsNode: TAstViewNode;
        FTransformNode: TAstViewNode;
    public
        procedure BuildUI(OwnerNode: TAstViewNode); override;
        function ReconstructAst(OwnerNode: TAstViewNode): IAstNode; override;
    end;

implementation

{ TPipeNodeHandler }

procedure TPipeNodeHandler.BuildUI(OwnerNode: TAstViewNode);
var
    visu: IAstVisualizer;
    header: TTextNode;
    arrow: TTextNode;
begin
    visu := OwnerNode.Visualizer.Clone(OwnerNode, OwnerNode.Visualizer.ExprDepth + 1);

    OwnerNode.Frameless := False;
    OwnerNode.Orientation := loVertical;
    OwnerNode.Alignment := laCenter;

    // Visuelles Styling für den Dataflow-Container
    OwnerNode.BackgroundColor := $FF202530;
    OwnerNode.BorderColor := TAlphaColors.Slateblue;

    // Header
    header := OwnerNode.AddLabel(OwnerNode, 'Pipe Pipeline');
    header.FontSettings.Font.Style := [TFontStyle.fsBold];
    header.Color := TAlphaColors.Lightskyblue;

    // Sektion 1: Inputs
    // FNode.Inputs ist jetzt ITupleNode. CallAccept triggert automatisch
    // den TTupleHandler aus Myc.Fmx.AstEditor.Handlers.Lists.
    FInputsNode := visu.CallAccept(FNode.Inputs);

    // Visueller Trenner
    arrow := OwnerNode.AddLabel(OwnerNode, '▼ Transform ▼');
    arrow.FontSettings.Font.Size := 10;
    arrow.Color := TAlphaColors.Gray;
    arrow.Margins := TMarginRect.Create(0, 5, 0, 5);

    // Sektion 2: Transformation (Lambda)
    FTransformNode := visu.CallAccept(FNode.Transformation);
end;

function TPipeNodeHandler.ReconstructAst(OwnerNode: TAstViewNode): IAstNode;
begin
    // Rekonstruktion nutzt das nun generische ITupleNode für die Inputs
    Result := TAst.Pipe(FNode.Identity, FInputsNode.CreateAst.AsTuple, FTransformNode.CreateAst.AsLambdaExpression, FNode.StaticType);
end;

end.
