from __future__ import annotations

from typing import Any

OP_NAME = "mul"


def uses_node_path(emitter: Any, node_spec: dict[str, Any]) -> bool:
    del emitter, node_spec
    return False


def interpret(
    model: Any,
    node_spec: dict[str, Any],
    env: dict[str, Any],
    *,
    node_path: str,
    scope: str,
    symbols: dict[str, int],
) -> None:
    inputs = node_spec.get("in")
    if not isinstance(inputs, list) or len(inputs) != 2:
        raise ValueError("mul expects two inputs")
    out = model._require_name(node_spec.get("out"), field="mul.out")
    env[out] = env[inputs[0]] * env[inputs[1]]
    return


def compile(
    emitter: Any,
    node_spec: dict[str, Any],
    env: dict[str, str],
    *,
    node_path_var: str,
    scope_var: str,
    indent: str,
) -> list[str]:
    lines: list[str] = []

    def assign_out_var(out_name: str) -> str:
        return emitter._assign_out_var(env, out_name)

    def infer_param(param_name: str) -> str:
        return emitter._infer_param_expr(node_spec, node_path_var, param_name)

    def read(name: str) -> str:
        return emitter._read_env_var(env, name)

    inputs = node_spec.get("in")
    if not isinstance(inputs, list) or len(inputs) != 2:
        raise ValueError("mul expects two inputs")
    a = read(str(inputs[0]))
    b = read(str(inputs[1]))
    out_name = str(node_spec.get("out"))
    out_var = assign_out_var(out_name)
    lines.append(f"{indent}{out_var} = {a} * {b}")
    return lines


__all__ = ["OP_NAME", "interpret", "compile", "uses_node_path"]
