<?php
/**
 * Language list for AI Alt Text (130+ languages).
 *
 * @package AIAltTextGenerator
 * @since 1.2.0
 */

namespace AIAltTextGenerator;

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

/**
 * Languages list.
 *
 * @since 1.2.0
 */
class Languages {

	/**
	 * Get language options for dropdown.
	 *
	 * @return array<string, string> Code => display name.
	 */
	public static function getList(): array {
		return array_merge(
			[
				'' => __( 'Auto-detect from site or post', 'wp-powersuite' ),
			],
			self::supportedLanguages()
		);
	}

	/**
	 * Human-readable label for prompts and UI.
	 */
	public static function getLabel( string $code ): string {
		$code = strtolower( trim( $code ) );
		if ( $code === '' ) {
			return '';
		}

		$languages = self::supportedLanguages();

		return $languages[ $code ] ?? $code;
	}

	/**
	 * Normalize stored language value (codes + legacy display names).
	 */
	public static function sanitize( string $value ): string {
		$value = trim( $value );
		if ( $value === '' ) {
			return '';
		}

		$code = strtolower( $value );
		if ( isset( self::supportedLanguages()[ $code ] ) ) {
			return $code;
		}

		$legacy = self::legacyNameToCode();
		if ( isset( $legacy[ $value ] ) ) {
			return $legacy[ $value ];
		}

		$lower_value = strtolower( $value );
		foreach ( $legacy as $name => $legacy_code ) {
			if ( strtolower( $name ) === $lower_value ) {
				return $legacy_code;
			}
		}

		return '';
	}

	/**
	 * @return array<string, string>
	 */
	private static function supportedLanguages(): array {
		return [
			'af'       => 'Afrikaans',
			'sq'       => 'Albanian',
			'am'       => 'Amharic',
			'ar'       => 'Arabic',
			'hy'       => 'Armenian',
			'as'       => 'Assamese',
			'ay'       => 'Aymara',
			'az'       => 'Azerbaijani',
			'bm'       => 'Bambara',
			'eu'       => 'Basque',
			'be'       => 'Belarusian',
			'bn'       => 'Bengali',
			'bho'      => 'Bhojpuri',
			'bs'       => 'Bosnian',
			'bg'       => 'Bulgarian',
			'ca'       => 'Catalan',
			'ceb'      => 'Cebuano',
			'zh-cn'    => 'Chinese (Simplified)',
			'zh-tw'    => 'Chinese (Traditional)',
			'co'       => 'Corsican',
			'hr'       => 'Croatian',
			'cs'       => 'Czech',
			'da'       => 'Danish',
			'dv'       => 'Dhivehi',
			'doi'      => 'Dogri',
			'nl'       => 'Dutch',
			'en'       => 'English (American)',
			'en-gb'    => 'English (British)',
			'eo'       => 'Esperanto',
			'et'       => 'Estonian',
			'ee'       => 'Ewe',
			'fil'      => 'Filipino (Tagalog)',
			'fi'       => 'Finnish',
			'fr'       => 'French',
			'fy'       => 'Frisian',
			'gl'       => 'Galician',
			'ka'       => 'Georgian',
			'de'       => 'German',
			'el'       => 'Greek',
			'gn'       => 'Guarani',
			'gu'       => 'Gujarati',
			'ht'       => 'Haitian Creole',
			'ha'       => 'Hausa',
			'haw'      => 'Hawaiian',
			'he'       => 'Hebrew',
			'hi'       => 'Hindi',
			'hmn'      => 'Hmong',
			'hu'       => 'Hungarian',
			'is'       => 'Icelandic',
			'ig'       => 'Igbo',
			'ilo'      => 'Ilocano',
			'id'       => 'Indonesian',
			'ga'       => 'Irish',
			'it'       => 'Italian',
			'ja'       => 'Japanese',
			'jv'       => 'Javanese',
			'kn'       => 'Kannada',
			'kk'       => 'Kazakh',
			'km'       => 'Khmer',
			'rw'       => 'Kinyarwanda',
			'gom'      => 'Konkani',
			'ko'       => 'Korean',
			'kri'      => 'Krio',
			'ku'       => 'Kurdish',
			'ckb'      => 'Kurdish (Sorani)',
			'ky'       => 'Kyrgyz',
			'lo'       => 'Lao',
			'la'       => 'Latin',
			'lv'       => 'Latvian',
			'ln'       => 'Lingala',
			'lt'       => 'Lithuanian',
			'lg'       => 'Luganda',
			'lb'       => 'Luxembourgish',
			'mk'       => 'Macedonian',
			'mai'      => 'Maithili',
			'mg'       => 'Malagasy',
			'ms'       => 'Malay',
			'ml'       => 'Malayalam',
			'mt'       => 'Maltese',
			'mi'       => 'Maori',
			'mr'       => 'Marathi',
			'mni-mtei' => 'Meiteilon (Manipuri)',
			'lus'      => 'Mizo',
			'mn'       => 'Mongolian',
			'my'       => 'Myanmar (Burmese)',
			'ne'       => 'Nepali',
			'no'       => 'Norwegian',
			'ny'       => 'Nyanja (Chichewa)',
			'or'       => 'Odia (Oriya)',
			'om'       => 'Oromo',
			'ps'       => 'Pashto',
			'fa'       => 'Persian',
			'pl'       => 'Polish',
			'pt'       => 'Portuguese (Brazil)',
			'pt-pt'    => 'Portuguese (Portugal)',
			'pa'       => 'Punjabi',
			'qu'       => 'Quechua',
			'ro'       => 'Romanian',
			'ru'       => 'Russian',
			'sm'       => 'Samoan',
			'sa'       => 'Sanskrit',
			'gd'       => 'Scots Gaelic',
			'nso'      => 'Sepedi',
			'sr'       => 'Serbian',
			'st'       => 'Sesotho',
			'sn'       => 'Shona',
			'sd'       => 'Sindhi',
			'si'       => 'Sinhala (Sinhalese)',
			'sk'       => 'Slovak',
			'sl'       => 'Slovenian',
			'so'       => 'Somali',
			'es'       => 'Spanish',
			'su'       => 'Sundanese',
			'sw'       => 'Swahili',
			'sv'       => 'Swedish',
			'tl'       => 'Tagalog (Filipino)',
			'tg'       => 'Tajik',
			'ta'       => 'Tamil',
			'tt'       => 'Tatar',
			'te'       => 'Telugu',
			'th'       => 'Thai',
			'ti'       => 'Tigrinya',
			'ts'       => 'Tsonga',
			'tr'       => 'Turkish',
			'tk'       => 'Turkmen',
			'ak'       => 'Twi (Akan)',
			'uk'       => 'Ukrainian',
			'ur'       => 'Urdu',
			'ug'       => 'Uyghur',
			'uz'       => 'Uzbek',
			'vi'       => 'Vietnamese',
			'cy'       => 'Welsh',
			'xh'       => 'Xhosa',
			'yi'       => 'Yiddish',
			'yo'       => 'Yoruba',
			'zu'       => 'Zulu',
		];
	}

	/**
	 * Map legacy stored display names to language codes.
	 *
	 * @return array<string, string>
	 */
	private static function legacyNameToCode(): array {
		return [
			'English (American)' => 'en',
			'English (British)'  => 'en-gb',
			'English'            => 'en',
			'Spanish'            => 'es',
			'French'             => 'fr',
			'German'             => 'de',
			'Italian'            => 'it',
			'Portuguese'         => 'pt',
			'Dutch'              => 'nl',
			'Polish'             => 'pl',
			'Russian'            => 'ru',
			'Japanese'           => 'ja',
			'Chinese'            => 'zh-cn',
			'Korean'             => 'ko',
			'Arabic'             => 'ar',
			'Hindi'              => 'hi',
			'Turkish'            => 'tr',
			'Vietnamese'         => 'vi',
			'Thai'               => 'th',
			'Indonesian'         => 'id',
			'Malay'              => 'ms',
			'Swedish'            => 'sv',
			'Danish'             => 'da',
			'Norwegian'          => 'no',
			'Finnish'            => 'fi',
			'Greek'              => 'el',
			'Czech'              => 'cs',
			'Romanian'           => 'ro',
			'Hungarian'          => 'hu',
			'Ukrainian'          => 'uk',
			'Hebrew'             => 'he',
			'Persian'            => 'fa',
			'Bengali'            => 'bn',
			'Tamil'              => 'ta',
			'Telugu'             => 'te',
			'Marathi'            => 'mr',
			'Gujarati'           => 'gu',
			'Kannada'            => 'kn',
			'Malayalam'          => 'ml',
			'Punjabi'            => 'pa',
			'Urdu'               => 'ur',
			'Swahili'            => 'sw',
			'Afrikaans'          => 'af',
			'Catalan'            => 'ca',
			'Bulgarian'          => 'bg',
			'Croatian'           => 'hr',
			'Slovak'             => 'sk',
			'Slovenian'          => 'sl',
			'Serbian'            => 'sr',
			'Lithuanian'         => 'lt',
			'Latvian'            => 'lv',
			'Estonian'           => 'et',
			'Icelandic'          => 'is',
			'Irish'              => 'ga',
			'Welsh'              => 'cy',
			'Scottish Gaelic'    => 'gd',
			'Basque'             => 'eu',
			'Galician'           => 'gl',
		];
	}
}
