<?php

/**
 * Vala-IR statement for rethrowing an already-caught Vala host Error.
 *
 * PHP `throw` semantics are lowered before codegen to ordinary runtime call
 * statements such as PhpefRuntimeCore.throw_exception(...).
 */
final class ValaIrThrowStatement
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private string $hostErrorName;

    /**
     * Create a host Error rethrow statement.
     *
     * @param int $startLine Source start line.
     * @param int $endLine Source end line.
     * @param array $comments Vala-IR comments attached to the rethrow statement.
     * @param string $hostErrorName Existing host Error variable for rethrow.
     */
    public function __construct(int $startLine, int $endLine, array $comments, string $hostErrorName)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->hostErrorName = $hostErrorName;
    }

    /** Create a Vala host Error rethrow statement. */
    public static function hostErrorRethrow(int $startLine, int $endLine, array $comments, string $hostErrorName): self
    {
        return new self($startLine, $endLine, $comments, $hostErrorName);
    }

    /**
     * Convert to a plain array for debugging/export.
     */
    public function toArray(): array
    {
        return [
            'kind' => 'ThrowStatement',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (ValaIrComment $comment): array => $comment->toArray(), $this->comments),
            'hostErrorName' => $this->hostErrorName,
        ];
    }
}
