<?php

namespace NoahVet\Reef\Factory\RequestContext;

use Symfony\Component\HttpFoundation\Request;

class SortingApplier extends AbstractApplier implements SortingApplierInterface
{
    /**
     * @return array<string, mixed>
     */
    #[\Override]
    public function apply(Request $request, array $whitelist): array
    {
        $allowedSortingKeys = \array_flip($whitelist['sorting']);

        /** @var array<string, scalar> $sortingKeys */
        $sortingKeys = \array_filter(
            $request->query->all(),
            static fn ($queryParam) => \str_starts_with($queryParam, 'sort') && \array_key_exists($queryParam, $allowedSortingKeys),
            \ARRAY_FILTER_USE_KEY,
        );

        return $sortingKeys;
    }
}
