<?php

/**
 * Vala-IR item inside a Zval array literal. A null key means auto-append.
 */
final class ValaIrArrayItem
{
    private int $startLine;
    private int $endLine;
    private array $comments;
    private ?object $key;
    private object $value;

    /**
     * Create the IR node.
     */
    public function __construct(int $startLine, int $endLine, array $comments, ?object $key, object $value)
    {
        $this->startLine = $startLine;
        $this->endLine = $endLine;
        $this->comments = $comments;
        $this->key = $key;
        $this->value = $value;
    }

    /**
     * Convert to a plain array for debugging/export.
     */
    public function toArray(): array
    {
        return [
            'kind' => 'ArrayItem',
            'startLine' => $this->startLine,
            'endLine' => $this->endLine,
            'comments' => array_map(static fn (ValaIrComment $comment): array => $comment->toArray(), $this->comments),
            'key' => $this->key !== null && method_exists($this->key, 'toArray') ? $this->key->toArray() : null,
            'value' => method_exists($this->value, 'toArray') ? $this->value->toArray() : ['kind' => get_debug_type($this->value)],
        ];
    }
}
