<?php

declare(strict_types=1);

namespace App\Core\Http;

final class RedirectPathResolver
{
    /**
     * @param string $requestedPath Raw path from user input
     * @param array<int, string> $allowedPaths Prefixes that the path must start with
     * @param string $default Fallback path when validation fails
     */
    public static function resolve(string $requestedPath, array $allowedPaths, string $default): string
    {
        $value = trim($requestedPath);
        if ($value === '') {
            return $default;
        }

        foreach ($allowedPaths as $prefix) {
            if (str_starts_with($value, $prefix)) {
                return $value;
            }
        }

        return $default;
    }
}
