<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Lang;
use TechnoArtisan\FilamentCms\FilamentCms;

if (!function_exists('obfuscateString')) {
    function obfuscateString(string $string): string
    {
        $obfuscated = '';
        foreach (mb_str_split($string) as $char) {
            if (in_array($char, ['ä', 'ö', 'ü'])) {
                switch ($char) {
                    case 'ä':
                        $char = '&auml;';
                        break;
                    case 'ö':
                        $char = '&ouml;';
                        break;
                    case 'ü':
                        $char = '&uuml;';
                        break;
                }
                $obfuscated .= $char;
                continue;
            }
            $max = 3;

            if ($char === ' ') {
                $max = 4;
            }

            try {
                switch (random_int(1, $max)) {
                    // HTML HEX
                    case 1:
                        $obfuscated .= '&#x' . dechex(ord($char)) . ';';
                        break;

                    // HTML DEC
                    case 2:
                        $obfuscated .= '&#' . ord($char) . ';';
                        break;

                    // HTML NATIVE
                    case 3:
                        $obfuscated .= $char;
                        break;

                    // only for whitespaces
                    case 4:
                        $obfuscated .= '&nbsp;';
                        break;
                }
            } catch (Throwable $exception) {
                $obfuscated .= $char;
            }
        }

        return $obfuscated;
    }
}

if (!function_exists('getPageKeyFromLocalizedPage')) {
    function getPageKeyFromLocalizedPage(string $page): string
    {
        static $localizedRoutes = array_flip(Lang::get(FilamentCms::getPackageName() . '::routes'));

        if (!isset($localizedRoutes[$page])) {
            abort(404);
        }

        return $localizedRoutes[$page];
    }
}

if (!function_exists('getPreparedLanguageSelectorData')) {
    function getPreparedLanguageSelectorData(string $currentPageKey): array
    {
        static $availableLocales = config('filament-cms.locale.available');

        $data = [];

        foreach ($availableLocales as $locale) {
            $data[$locale] = $currentPageKey;
        }

        return $data;
    }
}

if (!function_exists('droute')) {
    function droute($page, $parameters = [], $absolute = false, array $replace = [], string|null $locale = null): string
    {
        return route(
            'all',
            // load localized routes
            $parameters + ['page' => fcms__('routes.' . $page, $replace, $locale)],
            $absolute
        );
    }
}

if (!function_exists('fcms__')) {
    function fcms__(string|null $key = null, array $replace = [], string|null $locale = null): string|array|null
    {
        return __(FilamentCms::getPackageName() . '::' . $key, $replace, $locale);
    }
}
