<?php

declare(strict_types=1);

namespace Signocore\Multilanguage\Support;

if (!defined('ABSPATH')) {
	exit;
}

final class LanguageHelper
{
	/**
	 * Master list of available languages.
	 *
	 * @return array<string, array{code: string, locale: string, name: string, native_name: string, rtl: bool}>
	 */
	public static function getAllLanguages(): array
	{
		return [
			'af' => ['code' => 'af', 'locale' => 'af', 'name' => 'Afrikaans', 'native_name' => 'Afrikaans', 'rtl' => false],
			'ar' => ['code' => 'ar', 'locale' => 'ar', 'name' => 'Arabic', 'native_name' => "\u{0627}\u{0644}\u{0639}\u{0631}\u{0628}\u{064A}\u{0629}", 'rtl' => true],
			'az' => ['code' => 'az', 'locale' => 'az', 'name' => 'Azerbaijani', 'native_name' => "Az\u{0259}rbaycan dili", 'rtl' => false],
			'be' => ['code' => 'be', 'locale' => 'bel', 'name' => 'Belarusian', 'native_name' => "\u{0411}\u{0435}\u{043B}\u{0430}\u{0440}\u{0443}\u{0441}\u{043A}\u{0430}\u{044F}", 'rtl' => false],
			'bg' => ['code' => 'bg', 'locale' => 'bg_BG', 'name' => 'Bulgarian', 'native_name' => "\u{0411}\u{044A}\u{043B}\u{0433}\u{0430}\u{0440}\u{0441}\u{043A}\u{0438}", 'rtl' => false],
			'bn' => ['code' => 'bn', 'locale' => 'bn_BD', 'name' => 'Bengali', 'native_name' => "\u{09AC}\u{09BE}\u{0982}\u{09B2}\u{09BE}", 'rtl' => false],
			'bs' => ['code' => 'bs', 'locale' => 'bs_BA', 'name' => 'Bosnian', 'native_name' => 'Bosanski', 'rtl' => false],
			'ca' => ['code' => 'ca', 'locale' => 'ca', 'name' => 'Catalan', 'native_name' => "Catal\u{00E0}", 'rtl' => false],
			'cs' => ['code' => 'cs', 'locale' => 'cs_CZ', 'name' => 'Czech', 'native_name' => "\u{010C}e\u{0161}tina", 'rtl' => false],
			'cy' => ['code' => 'cy', 'locale' => 'cy', 'name' => 'Welsh', 'native_name' => 'Cymraeg', 'rtl' => false],
			'da' => ['code' => 'da', 'locale' => 'da_DK', 'name' => 'Danish', 'native_name' => 'Dansk', 'rtl' => false],
			'de' => ['code' => 'de', 'locale' => 'de_DE', 'name' => 'German', 'native_name' => 'Deutsch', 'rtl' => false],
			'el' => ['code' => 'el', 'locale' => 'el', 'name' => 'Greek', 'native_name' => "\u{0395}\u{03BB}\u{03BB}\u{03B7}\u{03BD}\u{03B9}\u{03BA}\u{03AC}", 'rtl' => false],
			'en' => ['code' => 'en', 'locale' => 'en_US', 'name' => 'English', 'native_name' => 'English', 'rtl' => false],
			'es' => ['code' => 'es', 'locale' => 'es_ES', 'name' => 'Spanish', 'native_name' => "Espa\u{00F1}ol", 'rtl' => false],
			'et' => ['code' => 'et', 'locale' => 'et', 'name' => 'Estonian', 'native_name' => 'Eesti', 'rtl' => false],
			'eu' => ['code' => 'eu', 'locale' => 'eu', 'name' => 'Basque', 'native_name' => 'Euskara', 'rtl' => false],
			'fa' => ['code' => 'fa', 'locale' => 'fa_IR', 'name' => 'Persian', 'native_name' => "\u{0641}\u{0627}\u{0631}\u{0633}\u{06CC}", 'rtl' => true],
			'fi' => ['code' => 'fi', 'locale' => 'fi', 'name' => 'Finnish', 'native_name' => 'Suomi', 'rtl' => false],
			'fo' => ['code' => 'fo', 'locale' => 'fo', 'name' => 'Faroese', 'native_name' => "F\u{00F8}royskt", 'rtl' => false],
			'fr' => ['code' => 'fr', 'locale' => 'fr_FR', 'name' => 'French', 'native_name' => "Fran\u{00E7}ais", 'rtl' => false],
			'ga' => ['code' => 'ga', 'locale' => 'ga', 'name' => 'Irish', 'native_name' => 'Gaeilge', 'rtl' => false],
			'gl' => ['code' => 'gl', 'locale' => 'gl_ES', 'name' => 'Galician', 'native_name' => 'Galego', 'rtl' => false],
			'he' => ['code' => 'he', 'locale' => 'he_IL', 'name' => 'Hebrew', 'native_name' => "\u{05E2}\u{05D1}\u{05E8}\u{05D9}\u{05EA}", 'rtl' => true],
			'hi' => ['code' => 'hi', 'locale' => 'hi_IN', 'name' => 'Hindi', 'native_name' => "\u{0939}\u{093F}\u{0928}\u{094D}\u{0926}\u{0940}", 'rtl' => false],
			'hr' => ['code' => 'hr', 'locale' => 'hr', 'name' => 'Croatian', 'native_name' => 'Hrvatski', 'rtl' => false],
			'hu' => ['code' => 'hu', 'locale' => 'hu_HU', 'name' => 'Hungarian', 'native_name' => 'Magyar', 'rtl' => false],
			'hy' => ['code' => 'hy', 'locale' => 'hy', 'name' => 'Armenian', 'native_name' => "\u{0540}\u{0561}\u{0575}\u{0565}\u{0580}\u{0565}\u{0576}", 'rtl' => false],
			'id' => ['code' => 'id', 'locale' => 'id_ID', 'name' => 'Indonesian', 'native_name' => 'Bahasa Indonesia', 'rtl' => false],
			'is' => ['code' => 'is', 'locale' => 'is_IS', 'name' => 'Icelandic', 'native_name' => "\u{00CD}slenska", 'rtl' => false],
			'it' => ['code' => 'it', 'locale' => 'it_IT', 'name' => 'Italian', 'native_name' => 'Italiano', 'rtl' => false],
			'ja' => ['code' => 'ja', 'locale' => 'ja', 'name' => 'Japanese', 'native_name' => "\u{65E5}\u{672C}\u{8A9E}", 'rtl' => false],
			'ka' => ['code' => 'ka', 'locale' => 'ka_GE', 'name' => 'Georgian', 'native_name' => "\u{10E5}\u{10D0}\u{10E0}\u{10D7}\u{10E3}\u{10DA}\u{10D8}", 'rtl' => false],
			'kk' => ['code' => 'kk', 'locale' => 'kk', 'name' => 'Kazakh', 'native_name' => "\u{049A}\u{0430}\u{0437}\u{0430}\u{049B} \u{0442}\u{0456}\u{043B}\u{0456}", 'rtl' => false],
			'km' => ['code' => 'km', 'locale' => 'km', 'name' => 'Khmer', 'native_name' => "\u{1797}\u{17B6}\u{179F}\u{17B6}\u{1781}\u{17D2}\u{1798}\u{17C2}\u{179A}", 'rtl' => false],
			'ko' => ['code' => 'ko', 'locale' => 'ko_KR', 'name' => 'Korean', 'native_name' => "\u{D55C}\u{AD6D}\u{C5B4}", 'rtl' => false],
			'ku' => ['code' => 'ku', 'locale' => 'ckb', 'name' => 'Kurdish', 'native_name' => "\u{06A9}\u{0648}\u{0631}\u{062F}\u{06CC}", 'rtl' => true],
			'lt' => ['code' => 'lt', 'locale' => 'lt_LT', 'name' => 'Lithuanian', 'native_name' => "Lietuvi\u{0173} kalba", 'rtl' => false],
			'lv' => ['code' => 'lv', 'locale' => 'lv', 'name' => 'Latvian', 'native_name' => "Latvie\u{0161}u valoda", 'rtl' => false],
			'mk' => ['code' => 'mk', 'locale' => 'mk_MK', 'name' => 'Macedonian', 'native_name' => "\u{041C}\u{0430}\u{043A}\u{0435}\u{0434}\u{043E}\u{043D}\u{0441}\u{043A}\u{0438}", 'rtl' => false],
			'mn' => ['code' => 'mn', 'locale' => 'mn', 'name' => 'Mongolian', 'native_name' => "\u{041C}\u{043E}\u{043D}\u{0433}\u{043E}\u{043B}", 'rtl' => false],
			'ms' => ['code' => 'ms', 'locale' => 'ms_MY', 'name' => 'Malay', 'native_name' => 'Bahasa Melayu', 'rtl' => false],
			'mt' => ['code' => 'mt', 'locale' => 'mt_MT', 'name' => 'Maltese', 'native_name' => 'Malti', 'rtl' => false],
			'nb' => ['code' => 'nb', 'locale' => 'nb_NO', 'name' => 'Norwegian', 'native_name' => 'Norsk', 'rtl' => false],
			'ne' => ['code' => 'ne', 'locale' => 'ne_NP', 'name' => 'Nepali', 'native_name' => "\u{0928}\u{0947}\u{092A}\u{093E}\u{0932}\u{0940}", 'rtl' => false],
			'nl' => ['code' => 'nl', 'locale' => 'nl_NL', 'name' => 'Dutch', 'native_name' => 'Nederlands', 'rtl' => false],
			'pl' => ['code' => 'pl', 'locale' => 'pl_PL', 'name' => 'Polish', 'native_name' => 'Polski', 'rtl' => false],
			'pt-br' => ['code' => 'pt-br', 'locale' => 'pt_BR', 'name' => 'Portuguese (Brazil)', 'native_name' => "Portugu\u{00EA}s (Brasil)", 'rtl' => false],
			'pt' => ['code' => 'pt', 'locale' => 'pt_PT', 'name' => 'Portuguese', 'native_name' => "Portugu\u{00EA}s", 'rtl' => false],
			'ro' => ['code' => 'ro', 'locale' => 'ro_RO', 'name' => 'Romanian', 'native_name' => "Rom\u{00E2}n\u{0103}", 'rtl' => false],
			'ru' => ['code' => 'ru', 'locale' => 'ru_RU', 'name' => 'Russian', 'native_name' => "\u{0420}\u{0443}\u{0441}\u{0441}\u{043A}\u{0438}\u{0439}", 'rtl' => false],
			'sk' => ['code' => 'sk', 'locale' => 'sk_SK', 'name' => 'Slovak', 'native_name' => "Sloven\u{010D}ina", 'rtl' => false],
			'sl' => ['code' => 'sl', 'locale' => 'sl_SI', 'name' => 'Slovenian', 'native_name' => "Sloven\u{0161}\u{010D}ina", 'rtl' => false],
			'sq' => ['code' => 'sq', 'locale' => 'sq', 'name' => 'Albanian', 'native_name' => 'Shqip', 'rtl' => false],
			'sr' => ['code' => 'sr', 'locale' => 'sr_RS', 'name' => 'Serbian', 'native_name' => "\u{0421}\u{0440}\u{043F}\u{0441}\u{043A}\u{0438}", 'rtl' => false],
			'sv' => ['code' => 'sv', 'locale' => 'sv_SE', 'name' => 'Swedish', 'native_name' => 'Svenska', 'rtl' => false],
			'sw' => ['code' => 'sw', 'locale' => 'sw', 'name' => 'Swahili', 'native_name' => 'Kiswahili', 'rtl' => false],
			'ta' => ['code' => 'ta', 'locale' => 'ta_IN', 'name' => 'Tamil', 'native_name' => "\u{0BA4}\u{0BAE}\u{0BBF}\u{0BB4}\u{0BCD}", 'rtl' => false],
			'th' => ['code' => 'th', 'locale' => 'th', 'name' => 'Thai', 'native_name' => "\u{0E44}\u{0E17}\u{0E22}", 'rtl' => false],
			'tl' => ['code' => 'tl', 'locale' => 'tl', 'name' => 'Tagalog', 'native_name' => 'Tagalog', 'rtl' => false],
			'tr' => ['code' => 'tr', 'locale' => 'tr_TR', 'name' => 'Turkish', 'native_name' => "T\u{00FC}rk\u{00E7}e", 'rtl' => false],
			'uk' => ['code' => 'uk', 'locale' => 'uk', 'name' => 'Ukrainian', 'native_name' => "\u{0423}\u{043A}\u{0440}\u{0430}\u{0457}\u{043D}\u{0441}\u{044C}\u{043A}\u{0430}", 'rtl' => false],
			'ur' => ['code' => 'ur', 'locale' => 'ur', 'name' => 'Urdu', 'native_name' => "\u{0627}\u{0631}\u{062F}\u{0648}", 'rtl' => true],
			'uz' => ['code' => 'uz', 'locale' => 'uz_UZ', 'name' => 'Uzbek', 'native_name' => "O\u{02BB}zbek", 'rtl' => false],
			'vi' => ['code' => 'vi', 'locale' => 'vi', 'name' => 'Vietnamese', 'native_name' => "Ti\u{1EBF}ng Vi\u{1EC7}t", 'rtl' => false],
			'zh-hans' => ['code' => 'zh-hans', 'locale' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'native_name' => "\u{7B80}\u{4F53}\u{4E2D}\u{6587}", 'rtl' => false],
			'zh-hant' => ['code' => 'zh-hant', 'locale' => 'zh_TW', 'name' => 'Chinese (Traditional)', 'native_name' => "\u{7E41}\u{9AD4}\u{4E2D}\u{6587}", 'rtl' => false],
		];
	}

	/**
	 * @return array{code: string, locale: string, name: string, native_name: string, rtl: bool}|null
	 */
	public static function getByCode(string $code): ?array
	{
		return self::getAllLanguages()[$code] ?? null;
	}

	public static function isRTL(string $code): bool
	{
		$lang = self::getByCode($code);
		return $lang !== null && $lang['rtl'];
	}

	/**
	 * Detect the default language code from the WordPress locale.
	 */
	public static function detectDefaultLanguageCode(): string
	{
		$locale = get_locale();
		$languages = self::getAllLanguages();

		// Direct locale match
		foreach ($languages as $lang) {
			if ($lang['locale'] === $locale) {
				return $lang['code'];
			}
		}

		// Try matching just the language part (e.g., 'da' from 'da_DK')
		$parts = explode('_', $locale);
		$langPart = strtolower($parts[0]);

		if (isset($languages[$langPart])) {
			return $langPart;
		}

		return 'en';
	}
}
