<?php

/**
 * Vala-IR statement for if / else-if / else control flow.
 */
final class ValaIrIfStatement
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $condition;
    private array $stmts;
    private array $elseifs;
    private array $elseStmts;

    /**
     * Create a conditional statement.
     *
     * @param int $startLine Original PHP start line.
     * @param int $endLine Original PHP end line.
     * @param array $comments Vala-IR comments attached to the statement.
     * @param object $condition Vala bool expression.
     * @param array $stmts Vala-IR statements for the `if` branch.
     * @param array $elseifs List of ['condition' => object, 'stmts' => array] entries.
     * @param array $elseStmts Vala-IR statements for the optional `else` branch.
     */
    public function __construct(int $startLine, int $endLine, array $comments, object $condition, array $stmts, array $elseifs = [], array $elseStmts = [])
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->condition = $condition;
        $this->stmts = $stmts;
        $this->elseifs = $elseifs;
        $this->elseStmts = $elseStmts;
    }

    /** Return branch statements for optimizer passes. */
    public function stmts(): array
    {
        return $this->stmts;
    }

    /** Return elseif entries for optimizer passes. */
    public function elseifs(): array
    {
        return $this->elseifs;
    }

    /** Return else branch statements for optimizer passes. */
    public function elseStmts(): array
    {
        return $this->elseStmts;
    }

    /** Return a copy with updated branches. */
    public function withBranches(array $stmts, array $elseifs, array $elseStmts): self
    {
        return new self($this->startLine, $this->endLine, $this->comments, $this->condition, $stmts, $elseifs, $elseStmts);
    }

    /** Convert to a plain array for debugging/export. */
    public function toArray(): array
    {
        return [
            'kind' => 'IfStatement',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (ValaIrComment $comment): array => $comment->toArray(), $this->comments),
            'condition' => method_exists($this->condition, 'toArray') ? $this->condition->toArray() : ['kind' => get_debug_type($this->condition)],
            'stmts' => array_map(static fn (object $stmt): array => method_exists($stmt, 'toArray') ? $stmt->toArray() : ['kind' => get_debug_type($stmt)], $this->stmts),
            'elseifs' => array_map(static function (array $elseif): array {
                $condition = $elseif['condition'] ?? null;
                $stmts = is_array($elseif['stmts'] ?? null) ? $elseif['stmts'] : [];
                return [
                    'condition' => is_object($condition) && method_exists($condition, 'toArray') ? $condition->toArray() : ['kind' => get_debug_type($condition)],
                    'stmts' => array_map(static fn (object $stmt): array => method_exists($stmt, 'toArray') ? $stmt->toArray() : ['kind' => get_debug_type($stmt)], $stmts),
                ];
            }, $this->elseifs),
            'elseStmts' => array_map(static fn (object $stmt): array => method_exists($stmt, 'toArray') ? $stmt->toArray() : ['kind' => get_debug_type($stmt)], $this->elseStmts),
        ];
    }
}
