<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\DragDrop;

final class DesignerDragPayload
{
    public function __construct(
        private readonly string $sourceValue,
        private readonly string $typeValue,
        private readonly array $componentData = [],
        private readonly array $metaData = []
    ) {}

    public static function toolbox(string $componentType, array $meta = []): self
    {
        return new self('toolbox', $componentType, [], $meta);
    }

    public static function fromComponent(array $component, array $meta = []): self
    {
        return new self(
            'canvas',
            (string) ($component['type'] ?? 'component'),
            $component,
            $meta
        );
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['source'] ?? 'toolbox'),
            (string) ($data['type'] ?? $data['componentType'] ?? 'component'),
            (array) ($data['component'] ?? []),
            (array) ($data['meta'] ?? [])
        );
    }

    public function source(): string
    {
        return $this->sourceValue;
    }

    public function type(): string
    {
        return $this->typeValue;
    }

    public function component(): array
    {
        return $this->componentData;
    }

    public function meta(): array
    {
        return $this->metaData;
    }

    public function toArray(): array
    {
        return [
            'source' => $this->sourceValue,
            'type' => $this->typeValue,
            'component' => $this->componentData,
            'meta' => $this->metaData,
        ];
    }
}
