<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Query;

final class QueryPaginator
{
    public function __construct(
        private readonly int $page = 1,
        private readonly int $pageSize = 25
    ) {}

    public static function create(int $page = 1, int $pageSize = 25): self
    {
        return new self(max(1, $page), max(1, $pageSize));
    }

    public function page(): int { return max(1, $this->page); }
    public function pageSize(): int { return max(1, $this->pageSize); }

    public function slice(array $rows): array
    {
        return array_slice($rows, ($this->page() - 1) * $this->pageSize(), $this->pageSize());
    }

    public function toArray(): array
    {
        return ['page' => $this->page(), 'pageSize' => $this->pageSize()];
    }
}
