<?php

declare(strict_types=1);

namespace Ferienpass\AdminBundle\Tests\LiveComponent;

use Ferienpass\AdminBundle\LiveComponent\MultiSelect;
use Ferienpass\AdminBundle\LiveComponent\MultiSelectAction;
use PHPUnit\Framework\TestCase;

final class MultiSelectTest extends TestCase
{
    // ---- MultiSelectAction ----

    public function testMultiSelectActionStoresProperties(): void
    {
        $action = new MultiSelectAction('delete', '/delete', ['id' => 1], 'DELETE', 'modal');

        self::assertSame('delete', $action->name);
        self::assertSame('/delete', $action->route);
        self::assertSame(['id' => 1], $action->routeParameters);
        self::assertSame('DELETE', $action->method);
        self::assertSame('modal', $action->turboFrame);
    }

    public function testMultiSelectActionDefaultValues(): void
    {
        $action = new MultiSelectAction('confirm');

        self::assertNull($action->route);
        self::assertSame([], $action->routeParameters);
        self::assertSame('POST', $action->method);
        self::assertNull($action->turboFrame);
    }

    // ---- MultiSelect ----

    public function testMultiSelectWrapsStringActionsInMultiSelectAction(): void
    {
        $ms = new MultiSelect(['delete', 'archive'], 'MyHandler');

        $actions = $ms->getActions();
        self::assertCount(2, $actions);
        self::assertInstanceOf(MultiSelectAction::class, $actions[0]);
        self::assertSame('delete', $actions[0]->name);
    }

    public function testMultiSelectKeepsExistingMultiSelectActionInstances(): void
    {
        $action = new MultiSelectAction('export', '/export');
        $ms = new MultiSelect([$action], 'MyHandler');

        self::assertSame($action, $ms->getActions()[0]);
    }

    public function testMultiSelectGetHandler(): void
    {
        $ms = new MultiSelect([], 'SomeHandlerService');
        self::assertSame('SomeHandlerService', $ms->getHandler());
    }

    public function testMultiSelectGetPreferred(): void
    {
        $ms = new MultiSelect([], 'handler', ['delete']);
        self::assertSame(['delete'], $ms->getPreferred());
    }

    public function testMultiSelectDefaultPreferredIsEmpty(): void
    {
        $ms = new MultiSelect([], 'handler');
        self::assertSame([], $ms->getPreferred());
    }
}
