<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Component;

final class DesignerComponentInstance
{
    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 $bindings = [],
        private readonly array $events = [],
        private readonly array $resources = [],
        private readonly array $meta = []
    ) {}

    public static function create(string $id, DesignerComponentDefinition $definition, array $props = []): self
    {
        return new self(
            $id,
            $definition->type(),
            $definition->title(),
            array_replace_recursive($definition->defaultProperties(), $props),
            [],
            [],
            [],
            ['icons' => [], 'css' => [], 'js' => []],
            ['definitionVersion' => $definition->toArray()['version'] ?? '1.0.0']
        );
    }

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