<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Renderer;

final class DesignerRenderNode
{
    public function __construct(
        private readonly string $id,
        private readonly string $type,
        private readonly string $label = '',
        private readonly array $props = [],
        private readonly array $children = [],
        private readonly array $parts = [],
        private readonly array $meta = []
    ) {}

    public static function fromArray(array $component): self
    {
        return new self(
            (string) ($component['id'] ?? $component['name'] ?? 'component'),
            (string) ($component['type'] ?? 'component'),
            (string) ($component['label'] ?? $component['name'] ?? $component['type'] ?? 'Component'),
            (array) ($component['props'] ?? []),
            (array) ($component['children'] ?? []),
            (array) ($component['parts'] ?? []),
            (array) ($component['meta'] ?? [])
        );
    }

    public function id(): string { return $this->id; }
    public function type(): string { return $this->type; }
    public function label(): string { return $this->label; }
    public function props(): array { return $this->props; }
    public function children(): array { return $this->children; }
    public function parts(): array { return $this->parts; }
    public function meta(): array { return $this->meta; }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'type' => $this->type,
            'label' => $this->label,
            'props' => $this->props,
            'children' => $this->children,
            'parts' => $this->parts,
            'meta' => $this->meta,
        ];
    }
}
