<?php

declare(strict_types=1);

namespace PpPhp\Core\Designer\Selection;

final class DesignerSelectionTreeNode
{
    public function __construct(
        private readonly string $nodeId,
        private readonly string $label,
        private readonly string $type,
        private readonly array $target,
        private readonly array $children = [],
        private readonly bool $locked = false,
        private readonly bool $hidden = false
    ) {}

    public static function fromTarget(DesignerSelectionTarget $target, array $children = []): self
    {
        return new self(
            $target->targetId(),
            $target->label(),
            $target->part() ?: $target->componentType(),
            $target->toArray(),
            $children
        );
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['nodeId'] ?? ''),
            (string) ($data['label'] ?? ''),
            (string) ($data['type'] ?? ''),
            (array) ($data['target'] ?? []),
            (array) ($data['children'] ?? []),
            (bool) ($data['locked'] ?? false),
            (bool) ($data['hidden'] ?? false)
        );
    }

    public function toArray(): array
    {
        return [
            'nodeId' => $this->nodeId,
            'label' => $this->label,
            'type' => $this->type,
            'target' => $this->target,
            'children' => $this->children,
            'locked' => $this->locked,
            'hidden' => $this->hidden,
        ];
    }
}
