<?php

/**
 * PHP-IR statement for PHP while-loop control flow.
 */
final class PhpIrWhile
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $cond;
    private array $stmts;

    /**
     * Create a while-loop statement.
     *
     * @param int $startLine Original PHP start line.
     * @param int $endLine Original PHP end line.
     * @param array $comments PHP-IR comments attached to the loop.
     * @param object $cond PHP-IR condition expression.
     * @param array $stmts PHP-IR body statements.
     */
    public function __construct(int $startLine, int $endLine, array $comments, object $cond, array $stmts)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->cond = $cond;
        $this->stmts = $stmts;
    }

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