<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Component;

final class DesignerComponentFactory
{
    public function __construct(private readonly DesignerComponentRegistry $registry) {}

    public function create(string $type, string $id = '', array $props = []): DesignerComponentInstance
    {
        $definition = $this->registry->get($type);
        if (!$definition) {
            throw new \RuntimeException('Unknown Designer component type: ' . $type);
        }

        $id = $id !== '' ? $id : $type . '_' . substr(hash('sha256', $type . microtime(true)), 0, 8);
        return DesignerComponentInstance::create($id, $definition, $props);
    }
}
