<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Property;

final class DesignerPropertyEditorRegistry
{
    private array $editors = [];

    public function register(DesignerPropertyEditor $editor): void
    {
        $this->editors[$editor->type()] = $editor;
    }

    public function has(string $type): bool
    {
        return isset($this->editors[$type]);
    }

    public function get(string $type): ?DesignerPropertyEditor
    {
        return $this->editors[$type] ?? null;
    }

    public function all(): array
    {
        return $this->editors;
    }

    public static function defaults(): self
    {
        $registry = new self();

        foreach ([
            ['text', 'Text', 'text-input'],
            ['textarea', 'Textarea', 'textarea'],
            ['number', 'Number', 'number-input'],
            ['boolean', 'Boolean', 'toggle'],
            ['select', 'Select', 'select'],
            ['multiselect', 'Multi Select', 'multiselect'],
            ['color', 'Color', 'color-picker'],
            ['icon', 'Icon', 'icon-picker'],
            ['image', 'Image', 'image-picker'],
            ['spacing', 'Spacing', 'spacing-editor'],
            ['border', 'Border', 'border-editor'],
            ['shadow', 'Shadow', 'shadow-editor'],
            ['datasource', 'Datasource', 'datasource-picker'],
            ['workflow', 'Workflow', 'workflow-picker'],
            ['event', 'Event', 'event-editor'],
            ['code', 'Code', 'code-editor'],
            ['json', 'JSON', 'json-editor'],
            ['markdown', 'Markdown', 'markdown-editor'],
            ['readonly', 'Readonly', 'readonly'],
        ] as [$type, $label, $component]) {
            $registry->register(DesignerPropertyEditor::create($type, $label, $component));
        }

        return $registry;
    }
}
