<?php

/**
 * PHP-IR expression for PHP ternary conditional expressions.
 */
final class PhpIrTernary
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private object $condition;
    private object $ifExpr;
    private object $elseExpr;

    /** Create a ternary expression. */
    public function __construct(int $startLine, int $endLine, array $comments, object $condition, object $ifExpr, object $elseExpr)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->condition = $condition;
        $this->ifExpr = $ifExpr;
        $this->elseExpr = $elseExpr;
    }

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