<?php

/**
 * Vala-IR foreach over Zval array entries, assigning each value to a Zval variable.
 */
final class ValaIrForeachValueStatement
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $expr;
    private string $valueName;
    private string $entryName;
    private array $stmts;

    /**
     * Create a foreach-value Vala-IR statement.
     *
     *  array  Vala-IR comments attached to the foreach.
     *  array  Body statements.
     */
    public function __construct(int $startLine, int $endLine, array $comments, object $expr, string $valueName, string $entryName, array $stmts)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->expr = $expr;
        $this->valueName = $valueName;
        $this->entryName = $entryName;
        $this->stmts = $stmts;
    }

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