<?php

/**
 * Vala-IR expression for a typed Vala array creation such as new Zval[] { ... }.
 */
final class ValaIrArrayCreationExpression
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private string $elementTypeName;
    private array $items;

    /**
     * Create a typed Vala array creation expression.
     */
    public function __construct(int $startLine, int $endLine, array $comments, string $elementTypeName, array $items)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->elementTypeName = $elementTypeName;
        $this->items = $items;
    }

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