<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Toolbox;

final class DesignerToolboxGroup
{
    public function __construct(
        private readonly string $groupId,
        private readonly string $label,
        private readonly string $icon = '',
        private readonly array $items = [],
        private readonly int $order = 100
    ) {}

    public static function create(string $groupId, string $label, string $icon = '', array $items = [], int $order = 100): self
    {
        return new self($groupId, $label, $icon, $items, $order);
    }

    public function groupId(): string { return $this->groupId; }
    public function order(): int { return $this->order; }
    public function items(): array { return $this->items; }

    public function withItem(DesignerToolboxItem $item): self
    {
        return new self($this->groupId, $this->label, $this->icon, array_merge($this->items, [$item]), $this->order);
    }

    public function toArray(): array
    {
        return [
            'groupId' => $this->groupId,
            'label' => $this->label,
            'icon' => $this->icon,
            'order' => $this->order,
            'items' => array_map(static fn (DesignerToolboxItem $item): array => $item->toArray(), $this->items),
        ];
    }
}
