<?php

namespace App\Support;

final class LocaleOptions
{
    /** @return list<string> */
    public static function available(): array
    {
        $locales = config('localization.available', ['en', 'pt']);
        if (! is_array($locales)) {
            return ['en', 'pt'];
        }

        $out = [];
        foreach ($locales as $v) {
            if (is_string($v) && $v !== '') {
                $out[] = $v;
            }
        }

        $out = array_values(array_unique($out));

        return $out !== [] ? $out : ['en', 'pt'];
    }

    public static function isAvailable(string $locale): bool
    {
        return in_array($locale, self::available(), true);
    }

    public static function normalizeOrFallback(string $locale, ?string $fallback = null): string
    {
        $fallback ??= (string) config('app.fallback_locale', 'en');

        return self::isAvailable($locale) ? $locale : (self::isAvailable($fallback) ? $fallback : self::available()[0]);
    }
}
