<?php

/**
 * Vala-IR expression for a static/function-style call such as Zval.from_string(...).
 */
final class ValaIrStaticCallExpression
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private string $callee;
    private array $args;

    /**
     * Create a static/function-style call expression.
     */
    public function __construct(int $startLine, int $endLine, array $comments, string $callee, array $args)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->callee = $callee;
        $this->args = $args;
    }

    /**
     * Return a copy of this expression with replaced callee and arguments.
     */
    public function withCalleeAndArgs(string $callee, array $args): self
    {
        return new self($this->startLine, $this->endLine, $this->comments, $callee, $args);
    }

    /** Return this expression's argument nodes. */
    public function args(): array
    {
        return $this->args;
    }

    /**
     * Convert to a plain array for debugging/export.
     */
    public function toArray(): array
    {
        return [
            'kind' => 'StaticCallExpression',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (ValaIrComment $comment): array => $comment->toArray(), $this->comments),
            'callee' => $this->callee,
            'args' => array_map(
                static fn (object $arg): array => method_exists($arg, 'toArray') ? $arg->toArray() : ['kind' => get_debug_type($arg)],
                $this->args,
            ),
        ];
    }
}
