<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Contract;

final class DesignerSelectionModel
{
    public function __construct(
        private readonly ?string $componentId = null,
        private readonly ?string $part = null,
        private readonly array $path = [],
        private readonly array $meta = []
    ) {}

    public static function empty(): self
    {
        return new self();
    }

    public static function component(string $componentId, ?string $part = null, array $path = [], array $meta = []): self
    {
        return new self($componentId, $part, $path, $meta);
    }

    public static function fromArray(array $data): self
    {
        return new self(
            isset($data['componentId']) ? (string) $data['componentId'] : null,
            isset($data['part']) ? (string) $data['part'] : null,
            (array) ($data['path'] ?? []),
            (array) ($data['meta'] ?? [])
        );
    }

    public function isEmpty(): bool
    {
        return $this->componentId === null || $this->componentId === '';
    }

    public function componentId(): ?string
    {
        return $this->componentId;
    }

    public function part(): ?string
    {
        return $this->part;
    }

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

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

    public function toArray(): array
    {
        return [
            'componentId' => $this->componentId,
            'part' => $this->part,
            'path' => $this->path,
            'meta' => $this->meta,
        ];
    }
}
