<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DragDrop;

final class DesignerDragDropClipboard
{
    private array $clipboard = [];

    public function copy(array $component): array
    {
        $this->clipboard = [
            'operation' => 'copy',
            'component' => $component,
            'createdAt' => date('c'),
        ];

        return $this->clipboard;
    }

    public function cut(array $component): array
    {
        $this->clipboard = [
            'operation' => 'cut',
            'component' => $component,
            'createdAt' => date('c'),
        ];

        return $this->clipboard;
    }

    public function has(): bool
    {
        return isset($this->clipboard['component']) && is_array($this->clipboard['component']);
    }

    public function paste(string $newId = ''): ?array
    {
        if (!$this->has()) {
            return null;
        }

        $component = $this->clipboard['component'];
        $component['id'] = $newId !== '' ? $newId : (($component['id'] ?? 'component') . '_copy_1');
        $component['name'] = $component['id'];

        return $component;
    }

    public function toArray(): array
    {
        return $this->clipboard;
    }
}
