<?php
/**
 * Limit/offset helper for bounded read responses.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Support;

/**
 * Slices list payloads and reports truncation metadata.
 */
final class PaginatedList
{
    /**
     * @param array<int, mixed> $items
     * @return array{items: array<int, mixed>, total: int, truncated: bool, limit: int, offset: int}
     */
    public static function slice(array $items, int $limit = 50, int $offset = 0): array
    {
        $limit  = max(1, min($limit, 200));
        $offset = max(0, $offset);
        $total  = count($items);
        $slice  = array_slice($items, $offset, $limit);

        return [
            'items'     => array_values($slice),
            'total'     => $total,
            'truncated' => ($offset + count($slice)) < $total,
            'limit'     => $limit,
            'offset'    => $offset,
        ];
    }
}
