import asyncio
from typing import Union
from slyme.node import node, async_node, async_sequential_exec, Node, AsyncNode, async_sequential, Auto

from slyme.context import Context, Ref


@node
def node1(
    ctx: Context,
    /,
    *,
    value: Auto[list[str]],
):
    value.append("hello world!")
    return ctx


@async_node
async def node12(
    ctx: Context,
    /,
    *,
    value: Auto[list[str]],
):
    value.append("hello world!")
    return ctx


@async_node
async def node2(
    ctx: Context,
    /,
    *,
    nodes: list[Union[Node, AsyncNode]],
    value: Ref[list[str]],
):
    ctx = await async_sequential_exec(ctx, nodes)
    print(ctx.get(value))
    return ctx


scope = {
    "value": Ref("hello.value"),
}


root = async_sequential(nodes=[
    node12(scope),
    node1(scope),
    node2(scope, nodes=[node1(scope), node12(scope), node1(scope)]),
])
ctx = Context()
ctx = ctx.set(scope["value"], [])
aroot = root.prepare()
asyncio.run(aroot(ctx))
