<?php

namespace App\Twig\Components;

use App\Base\Util\Paginator;
use Doctrine\ORM\Query;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp;

trait PaginateTrait
{
    #[LiveProp(writable: true)]
    public int $pageNumber = 1;

    #[LiveProp(writable: true)]
    public int $perPage = 5;

    #[LiveAction]
    public function paginate(#[LiveArg] int $page)
    {
        $this->pageNumber = $page;
    }

    private function asPaginator(Query $query): Paginator
    {
        return (new Paginator())
            ->paginate($query, $this->pageNumber, $this->perPage);
    }
}
