<?php

declare(strict_types=1);

final class MyronPageRoleResolver
{
    public function __construct(private readonly MyronContentRepository $repository)
    {
    }

    /** @return array<string, mixed>|null */
    public function resolve(string $role, string $requestPath): ?array
    {
        $role = strtolower(trim($role));
        if (!in_array($role, MyronPageRoleService::ROLES, true)) {
            throw new InvalidArgumentException('Unknown page role: ' . $role);
        }
        $page = $this->repository->getPublishedPageByRole($role);
        if (!is_array($page)) {
            return null;
        }

        $requestPath = myronNormalizePath($requestPath);
        $page['_storedPath'] = (string) ($page['path'] ?? '');
        $page['_requestPath'] = $requestPath;
        $page['_pageRole'] = $role;
        $page['path'] = $requestPath;
        if (in_array($role, ['search', 'not-found'], true)) {
            $page['_forceNoindex'] = true;
        }
        return $page;
    }
}
