<?php

namespace App\Service;

use App\Config\LanguageCleanupConfig;

class FormatText
{
    public function __construct(private readonly LanguageCleanupConfig $languageCleanupConfig)
    {
    }

    function slugify(string $text): string
    {
        $text = mb_strtolower($text, 'UTF-8');

        // Use YAML-backed language normalization instead of a PHP-owned list.
        $replacements = $this->languageCleanupConfig->getAsciiTransliterationMap();
        $text = str_replace(array_keys($replacements), $replacements, $text);

        // Nicht erlaubte Zeichen entfernen
        $text = preg_replace('/[^a-z0-9\s.-]/', '', $text);

        // Leerzeichen zu Bindestrichen
        $text = preg_replace('/[\s-]+/', '-', $text);

        $text = preg_replace('/\./', '-', $text);

        return trim($text, '-');
    }
}
