<?php

declare(strict_types=1);

use Builder\Core\Foundation\Definition\MenuItem;

it('appendChild adds a child and returns self', function (): void {
    $parent = MenuItem::make('parent');
    $child = MenuItem::make('first');

    $result = $parent->appendChild($child);

    expect($result)->toBe($parent)
        ->and($parent->getChildren())->toHaveCount(1)
        ->and($parent->getChildren()[0]->getKey())->toBe('first');
});

it('appendChild preserves existing children', function (): void {
    $existing = MenuItem::make('existing');
    $parent = MenuItem::make('parent')->children([$existing]);

    $parent->appendChild(MenuItem::make('new'));

    expect($parent->getChildren())->toHaveCount(2)
        ->and(array_map(fn (MenuItem $c): string => $c->getKey(), $parent->getChildren()))
        ->toBe(['existing', 'new']);
});

it('appendChild is idempotent on child key', function (): void {
    $parent = MenuItem::make('parent');

    $parent->appendChild(MenuItem::make('same'));
    $parent->appendChild(MenuItem::make('same'));
    $parent->appendChild(MenuItem::make('same'));

    expect($parent->getChildren())->toHaveCount(1);
});
