<?php

/**
 * Root node for the first PHP-AST IR.
 */
final class PhpIrProgram
{
    private array $stmts;
    private string $phpSourceFile;
    private array $knownUserFunctionNames;
    private array $knownStaticMethodNames;
    private array $embeddedResourceReferences;

    /**
     * Create a program from statement nodes.
     */
    public function __construct(array $stmts, string $phpSourceFile = '', array $knownUserFunctionNames = [], array $knownStaticMethodNames = [], array $embeddedResourceReferences = [])
    {
        $this->stmts = $stmts;
        $this->phpSourceFile = $phpSourceFile;
        $this->knownUserFunctionNames = $knownUserFunctionNames;
        $this->knownStaticMethodNames = $knownStaticMethodNames;
        $this->embeddedResourceReferences = $embeddedResourceReferences;
    }

    /**
     * Convert to a plain array for debugging/export.
     */
    public function toArray(): array
    {
        return [
            'format' => 'phpast-ir-v0',
            'root' => [
                'kind' => 'Program',
                'phpSourceFile' => $this->phpSourceFile,
                'knownUserFunctionNames' => $this->knownUserFunctionNames,
                'knownStaticMethodNames' => $this->knownStaticMethodNames,
                'embeddedResourceReferences' => $this->embeddedResourceReferences,
                'stmts' => array_map(
                    static fn (object $stmt): array => method_exists($stmt, 'toArray') ? $stmt->toArray() : ['kind' => get_debug_type($stmt)],
                    $this->stmts,
                ),
            ],
        ];
    }
}
