<?php

namespace App\Twig\Components;

use App\Service\MangadexProvider;
use Doctrine\Common\Collections\Collection;
use Exception;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
class MangaSearch
{
    use DefaultActionTrait;

    #[LiveProp(writable: true)]
    public ?string $query = null;

    public function __construct(private readonly MangadexProvider $mangadexProvider)
    {
    }

    /**
     * @throws Exception
     */
    public function getMangas(): Collection|null
    {
        if ($this->query === null || $this->query === '') {
            return null;
        }

        return $this->mangadexProvider->search($this->query);
    }
}
