<?php
declare(strict_types=1);

namespace App\Theme;

use InvalidArgumentException;

final class LayoutPresetService
{
    /** @return array<string,list<string>> */
    public static function allowed(): array
    {
        return [
            'home' => ['community', 'qa_focus', 'compact'],
            'question' => ['classic', 'wide', 'sidebar'],
            'topic' => ['discussion', 'compact'],
            'header' => ['standard', 'compact'],
            'density' => ['compact', 'comfortable', 'spacious'],
            'sidebar_position' => ['start', 'end'],
        ];
    }

    public static function normalize(string $kind, mixed $value, ?string $fallback = null): string
    {
        $allowed = self::allowed()[$kind] ?? null;
        if ($allowed === null) throw new InvalidArgumentException('Unknown layout preset kind.');
        $value = strtolower(trim((string) $value));
        if (in_array($value, $allowed, true)) return $value;
        if ($fallback !== null && in_array($fallback, $allowed, true)) return $fallback;
        return $allowed[0];
    }
}
