<?php

namespace NoahVet\Reef\Factory\RequestContext;

use Symfony\Component\HttpFoundation\Request;

/**
 * @psalm-import-type WhitelistShape from \NoahVet\Reef\Factory\RequestContext\ApplierInterface
 * @psalm-import-type RequestContext from \NoahVet\Reef\Factory\RequestContext\RequestContextFactoryInterface
 */
class RequestContextFactory implements RequestContextFactoryInterface
{
    public function __construct(
        protected readonly PermissionApplierInterface $permissionApplier,
        protected readonly FilterApplierInterface $filterApplier,
        protected readonly SortingApplierInterface $sortingApplier,
        protected readonly KeywordApplierInterface $keywordApplier,
    ) {
    }

    /**
     * @param WhitelistShape $whitelist
     *
     * @return RequestContext
     */
    #[\Override]
    public function createContextFromRequest(Request $request, array $whitelist): array
    {
        return [
            'permissions' => $this->permissionApplier->apply($request, $whitelist),
            'filters' => $this->filterApplier->apply($request, $whitelist),
            'sorting' => $this->sortingApplier->apply($request, $whitelist),
            'keywords' => $this->keywordApplier->apply($request, $whitelist),
        ];
    }
}
