"""TransformNode — safe expression evaluation."""

from __future__ import annotations

from typing import TYPE_CHECKING, Literal

from core.pathways.expressions import safe_eval
from core.pathways.models.io import NodeInputs, NodeOutputs
from core.pathways.nodes.base import NodeBase

if TYPE_CHECKING:
    from core.pathways.context import Context


class TransformNode(NodeBase):
    """Expression transform — evaluates a safe expression over inputs.

    Attributes
    ----------
    expr : str
        Expression string using safe_expr language (e.g., ``"x * 2"``, ``"input"``).
    """

    type: Literal["transform"] = "transform"
    expr: str = "input"

    async def compute(self, inputs: NodeInputs, _ctx: Context) -> NodeOutputs:
        """Evaluate expr with inputs as environment."""
        result = safe_eval(self.expr, env=inputs.to_dict())
        return NodeOutputs(output=result)


__all__ = ["TransformNode"]
