<?php
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function bbcs_get_country_by_code($code) 
{
    $code = strtolower($code);
    return BBCS_COUNTRIES[$code] ?? BOTBLOCKER_EMPTY;
}

function bbcs_get_language_by_code($code) 
{
    $code = strtolower($code);
    return BBCS_LANGUAGES[$code] ?? BOTBLOCKER_EMPTY;
}

function bbcs_CountrylanguageMap()
{
    return BBCS_LANGUAGES_TO_LOCALE;
}

// Country codes
const BBCS_COUNTRIES = [
    'af' => 'Afghanistan',
    'ax' => 'Åland Islands',
    'al' => 'Albania',
    'dz' => 'Algeria',
    'as' => 'American Samoa',
    'ad' => 'Andorra',
    'ao' => 'Angola',
    'ai' => 'Anguilla',
    'aq' => 'Antarctica',
    'ag' => 'Antigua and Barbuda',
    'ar' => 'Argentina',
    'am' => 'Armenia',
    'aw' => 'Aruba',
    'au' => 'Australia',
    'at' => 'Austria',
    'az' => 'Azerbaijan',
    'bs' => 'Bahamas',
    'bh' => 'Bahrain',
    'bd' => 'Bangladesh',
    'bb' => 'Barbados',
    'by' => 'Belarus',
    'be' => 'Belgium',
    'bz' => 'Belize',
    'bj' => 'Benin',
    'bm' => 'Bermuda',
    'bt' => 'Bhutan',
    'bo' => 'Bolivia',
    'bq' => 'Bonaire, Sint Eustatius and Saba',
    'ba' => 'Bosnia and Herzegovina',
    'bw' => 'Botswana',
    'bv' => 'Bouvet Island',
    'br' => 'Brazil',
    'io' => 'British Indian Ocean Territory',
    'bn' => 'Brunei Darussalam',
    'bg' => 'Bulgaria',
    'bf' => 'Burkina Faso',
    'bi' => 'Burundi',
    'kh' => 'Cambodia',
    'cm' => 'Cameroon',
    'ca' => 'Canada',
    'cv' => 'Cape Verde',
    'ky' => 'Cayman Islands',
    'cf' => 'Central African Republic',
    'td' => 'Chad',
    'cl' => 'Chile',
    'cn' => 'China',
    'cx' => 'Christmas Island',
    'cc' => 'Cocos (Keeling) Islands',
    'co' => 'Colombia',
    'km' => 'Comoros',
    'cg' => 'Congo',
    'cd' => 'Congo, The Democratic Republic of',
    'ck' => 'Cook Islands',
    'cr' => 'Costa Rica',
    'ci' => 'Côte d\'ivoire',
    'hr' => 'Croatia',
    'cu' => 'Cuba',
    'cw' => 'Curaçao',
    'cy' => 'Cyprus',
    'cz' => 'Czech Republic',
    'dk' => 'Denmark',
    'dj' => 'Djibouti',
    'dm' => 'Dominica',
    'do' => 'Dominican Republic',
    'ec' => 'Ecuador',
    'eg' => 'Egypt',
    'sv' => 'El Salvador',
    'gq' => 'Equatorial Guinea',
    'er' => 'Eritrea',
    'ee' => 'Estonia',
    'et' => 'Ethiopia',
    'fk' => 'Falkland Islands (Malvinas)',
    'fo' => 'Faroe Islands',
    'fj' => 'Fiji',
    'fi' => 'Finland',
    'fr' => 'France',
    'gf' => 'French Guiana',
    'pf' => 'French Polynesia',
    'tf' => 'French Southern Territories',
    'ga' => 'Gabon',
    'gm' => 'Gambia',
    'ge' => 'Georgia',
    'de' => 'Germany',
    'gh' => 'Ghana',
    'gi' => 'Gibraltar',
    'gr' => 'Greece',
    'gl' => 'Greenland',
    'gd' => 'Grenada',
    'gp' => 'Guadeloupe',
    'gu' => 'Guam',
    'gt' => 'Guatemala',
    'gg' => 'Guernsey',
    'gn' => 'Guinea',
    'gw' => 'Guinea-Bissau',
    'gy' => 'Guyana',
    'ht' => 'Haiti',
    'hm' => 'Heard Island and McDonald Islands',
    'va' => 'Holy See (Vatican City State)',
    'hn' => 'Honduras',
    'hk' => 'Hong Kong',
    'hu' => 'Hungary',
    'is' => 'Iceland',
    'in' => 'India',
    'id' => 'Indonesia',
    'ir' => 'Iran, Islamic Republic of',
    'iq' => 'Iraq',
    'ie' => 'Ireland',
    'im' => 'Isle of Man',
    'il' => 'Israel',
    'it' => 'Italy',
    'jm' => 'Jamaica',
    'jp' => 'Japan',
    'je' => 'Jersey',
    'jo' => 'Jordan',
    'kz' => 'Kazakhstan',
    'ke' => 'Kenya',
    'ki' => 'Kiribati',
    'kp' => 'Korea, Democratic People\'s Republic of',
    'kr' => 'Korea, Republic of',
    'kw' => 'Kuwait',
    'kg' => 'Kyrgyzstan',
    'la' => 'Lao People\'s Democratic Republic',
    'lv' => 'Latvia',
    'lb' => 'Lebanon',
    'ls' => 'Lesotho',
    'lr' => 'Liberia',
    'ly' => 'Libya',
    'li' => 'Liechtenstein',
    'lt' => 'Lithuania',
    'lu' => 'Luxembourg',
    'mo' => 'Macao',
    'mk' => 'Macedonia, The Former Yugoslav Republic of',
    'mg' => 'Madagascar',
    'mw' => 'Malawi',
    'my' => 'Malaysia',
    'mv' => 'Maldives',
    'ml' => 'Mali',
    'mt' => 'Malta',
    'mh' => 'Marshall Islands',
    'mq' => 'Martinique',
    'mr' => 'Mauritania',
    'mu' => 'Mauritius',
    'yt' => 'Mayotte',
    'mx' => 'Mexico',
    'fm' => 'Micronesia, Federated States of',
    'md' => 'Moldova, Republic of',
    'mc' => 'Monaco',
    'mn' => 'Mongolia',
    'me' => 'Montenegro',
    'ms' => 'Montserrat',
    'ma' => 'Morocco',
    'mz' => 'Mozambique',
    'mm' => 'Myanmar',
    'na' => 'Namibia',
    'nr' => 'Nauru',
    'np' => 'Nepal',
    'nl' => 'Netherlands',
    'nc' => 'New Caledonia',
    'nz' => 'New Zealand',
    'ni' => 'Nicaragua',
    'ne' => 'Niger',
    'ng' => 'Nigeria',
    'nu' => 'Niue',
    'nf' => 'Norfolk Island',
    'mp' => 'Northern Mariana Islands',
    'no' => 'Norway',
    'om' => 'Oman',
    'pk' => 'Pakistan',
    'pw' => 'Palau',
    'ps' => 'Palestinian Territory, Occupied',
    'pa' => 'Panama',
    'pg' => 'Papua New Guinea',
    'py' => 'Paraguay',
    'pe' => 'Peru',
    'ph' => 'Philippines',
    'pn' => 'Pitcairn',
    'pl' => 'Poland',
    'pt' => 'Portugal',
    'pr' => 'Puerto Rico',
    'qa' => 'Qatar',
    're' => 'Réunion',
    'ro' => 'Romania',
    'ru' => 'Russian Federation',
    'rw' => 'Rwanda',
    'bl' => 'Saint Barthélemy',
    'sh' => 'Saint Helena, Ascension and Tristan da Cunha',
    'kn' => 'Saint Kitts and Nevis',
    'lc' => 'Saint Lucia',
    'mf' => 'Saint Martin (French Part)',
    'pm' => 'Saint Pierre and Miquelon',
    'vc' => 'Saint Vincent and The Grenadines',
    'ws' => 'Samoa',
    'sm' => 'San Marino',
    'st' => 'Sao Tome and Principe',
    'sa' => 'Saudi Arabia',
    'sn' => 'Senegal',
    'rs' => 'Serbia',
    'sc' => 'Seychelles',
    'sl' => 'Sierra Leone',
    'sg' => 'Singapore',
    'sk' => 'Slovakia',
    'sx' => 'Sint Maarten (Dutch Part)',
    'si' => 'Slovenia',
    'sb' => 'Solomon Islands',
    'so' => 'Somalia',
    'za' => 'South Africa',
    'gs' => 'South Georgia and The South Sandwich Islands',
    'ss' => 'South Sudan',
    'es' => 'Spain',
    'lk' => 'Sri Lanka',
    'sd' => 'Sudan',
    'sr' => 'Suriname',
    'sj' => 'Svalbard and Jan Mayen',
    'sz' => 'Swaziland',
    'se' => 'Sweden',
    'ch' => 'Switzerland',
    'sy' => 'Syrian Arab Republic',
    'tw' => 'Taiwan, Province of China',
    'tj' => 'Tajikistan',
    'tz' => 'Tanzania, United Republic of',
    'th' => 'Thailand',
    'tl' => 'Timor-Leste',
    'tg' => 'Togo',
    'tk' => 'Tokelau',
    'to' => 'Tonga',
    'tt' => 'Trinidad and Tobago',
    'tn' => 'Tunisia',
    'tr' => 'Turkey',
    'tm' => 'Turkmenistan',
    'tc' => 'Turks and Caicos Islands',
    'tv' => 'Tuvalu',
    'ug' => 'Uganda',
    'ua' => 'Ukraine',
    'ae' => 'United Arab Emirates',
    'gb' => 'United Kingdom',
    'us' => 'United States',
    'um' => 'United States Minor Outlying Islands',
    'uy' => 'Uruguay',
    'uz' => 'Uzbekistan',
    'vu' => 'Vanuatu',
    've' => 'Venezuela',
    'vn' => 'Viet Nam',
    'vg' => 'Virgin Islands, British',
    'vi' => 'Virgin Islands, U.S.',
    'wf' => 'Wallis and Futuna',
    'eh' => 'Western Sahara',
    'ye' => 'Yemen',
    'zm' => 'Zambia',
    'zw' => 'Zimbabwe'
];

// Language codes
const BBCS_LANGUAGES = [
    'af' => 'Afrikaans',
    'am' => 'Amharic',
    'ar' => 'Arabic',
    'as' => 'Assamese',
    'ba' => 'Bashkir',
    'be' => 'Belarusian',
    'bg' => 'Bulgarian',
    'bn' => 'Bengali',
    'bo' => 'Tibetan',
    'br' => 'Breton',
    'ca' => 'Catalan',
    'co' => 'Corsican',
    'cs' => 'Czech',
    'cy' => 'Welsh',
    'da' => 'Danish',
    'de' => 'German',
    'dv' => 'Divehi',
    'el' => 'Greek',
    'en' => 'English',
    'es' => 'Spanish',
    'et' => 'Estonian',
    'eu' => 'Basque',
    'fa' => 'Persian',
    'fi' => 'Finnish',
    'fo' => 'Faroese',
    'fr' => 'French',
    'gd' => 'Scottish Gaelic',
    'gl' => 'Galician',
    'gu' => 'Gujarati',
    'he' => 'Hebrew',
    'hi' => 'Hindi',
    'hr' => 'Croatian',
    'hu' => 'Hungarian',
    'hy' => 'Armenian',
    'id' => 'Indonesian',
    'ig' => 'Igbo',
    'is' => 'Icelandic',
    'it' => 'Italian',
    'ja' => 'Japanese',
    'ka' => 'Georgian',
    'kk' => 'Kazakh',
    'km' => 'Khmer',
    'kn' => 'Kannada',
    'ko' => 'Korean',
    'lb' => 'Luxembourgish',
    'lo' => 'Lao',
    'lt' => 'Lithuanian',
    'lv' => 'Latvian',
    'mi' => 'Maori',
    'ml' => 'Malayalam',
    'mr' => 'Marathi',
    'ms' => 'Malay',
    'mt' => 'Maltese',
    'ne' => 'Nepali',
    'nl' => 'Dutch',
    'no' => 'Norwegian',
    'oc' => 'Occitan',
    'or' => 'Oriya',
    'pl' => 'Polish',
    'ps' => 'Pashto',
    'pt' => 'Portuguese',
    'qu' => 'Quechua',
    'ro' => 'Romanian',
    'ru' => 'Russian',
    'rw' => 'Kinyarwanda',
    'sa' => 'Sanskrit',
    'si' => 'Sinhala',
    'sk' => 'Slovak',
    'sl' => 'Slovenian',
    'sq' => 'Albanian',
    'sv' => 'Swedish',
    'ta' => 'Tamil',
    'te' => 'Telugu',
    'th' => 'Thai',
    'tk' => 'Turkmen',
    'tr' => 'Turkish',
    'tt' => 'Tatar',
    'uk' => 'Ukrainian',
    'ur' => 'Urdu',
    'vi' => 'Vietnamese',
    'wo' => 'Wolof',
    'yo' => 'Yoruba',
    'zh' => 'Chinese',

    'arc' => 'Aramaic',
    'ber' => 'Berber languages',
    'bwg' => 'Chibarwe',
    'cal' => 'Carolinian',
    'crs' => 'Seselwa Creole French',
    'gil' => 'Gilbertese',
    'hgm' => 'Hiri Motu',
    'jam' => 'Jamaican Patois',
    'kck' => 'Kalanga',
    'mey' => 'Hassaniya',
    'mfe' => 'Mauritian Creole',
    'niu' => 'Niuean',
    'nrf' => 'Guernésiais/Jèrriais',
    'pih' => 'Pitcairn-Norfolk',
    'pov' => 'Upper Guinea Creole',
    'rar' => 'Rarotongan',
    'tet' => 'Tetum',
    'tkl' => 'Tokelauan',
    'tvl' => 'Tuvaluan',
    'toi' => 'Tonga (Zambezi)',
    'zib' => 'Zimbabwean Sign Language'    
];

// Mappings between countries and languages
const BBCS_LANGUAGES_TO_LOCALE = [
    'AD' => ['ca'],
    'AE' => ['ar'],
    'AF' => ['fa', 'ps', 'tk'],
    'AG' => ['en'],
    'AI' => ['en'],
    'AL' => ['sq'],
    'AM' => ['hy'],
    'AO' => ['pt'],
    'AR' => ['es', 'gn'],
    'AS' => ['en', 'sm'],
    'AT' => ['de'],
    'AU' => ['en'],
    'AW' => ['nl', 'pap'],
    'AX' => ['sv'],
    'AZ' => ['az'],
    'BA' => ['bs', 'hr', 'sr'],
    'BB' => ['en'],
    'BD' => ['bn'],
    'BE' => ['de', 'fr', 'nl'],
    'BF' => ['fr'],
    'BG' => ['bg'],
    'BH' => ['ar'],
    'BI' => ['fr', 'rn'],
    'BJ' => ['fr'],
    'BL' => ['fr'],
    'BM' => ['en'],
    'BN' => ['ms'],
    'BO' => ['ay', 'es', 'gn', 'qu'],
    'BQ' => ['en', 'nl', 'pap'],
    'BR' => ['pt'],
    'BS' => ['en'],
    'BT' => ['dz'],
    'BV' => ['no'],
    'BW' => ['en', 'tn'],
    'BY' => ['be', 'ru'],
    'BZ' => ['bjz', 'en', 'es'],
    'CA' => ['en', 'fr'],
    'CC' => ['en'],
    'CD' => ['fr', 'kg', 'ln', 'lua', 'sw'],
    'CF' => ['fr', 'sg'],
    'CG' => ['fr', 'kg', 'ln'],
    'CH' => ['de', 'fr', 'gsw', 'it', 'rm'],
    'CI' => ['fr'],
    'CK' => ['en', 'rar'],
    'CL' => ['es'],
    'CM' => ['en', 'fr'],
    'CN' => ['zh'],
    'CO' => ['es'],
    'CR' => ['es'],
    'CU' => ['es'],
    'CV' => ['pt'],
    'CW' => ['en', 'nl', 'pap'],
    'CX' => ['en'],
    'CY' => ['el', 'tr'],
    'CZ' => ['cs', 'sk'],
    'DE' => ['de'],
    'DJ' => ['ar', 'fr'],
    'DK' => ['da'],
    'DM' => ['en'],
    'DO' => ['es'],
    'DZ' => ['ar'],
    'EC' => ['es'],
    'EE' => ['et'],
    'EG' => ['ar'],
    'EH' => ['ber', 'es', 'mey'],
    'ER' => ['ar', 'en', 'ti'],
    'ES' => ['ca', 'es', 'eu', 'glc'],
    'ET' => ['am', 'om', 'ti'],
    'FI' => ['fi', 'sv'],
    'FJ' => ['en', 'fj', 'hif'],
    'FK' => ['en'],
    'FM' => ['en'],
    'FO' => ['da', 'fo'],
    'FR' => ['fr'],
    'GA' => ['fr'],
    'GB' => ['en'],
    'GD' => ['en'],
    'GE' => ['ka'],
    'GF' => ['fr'],
    'GG' => ['en', 'fr', 'nfr'],
    'GH' => ['en'],
    'GI' => ['en'],
    'GL' => ['kl'],
    'GM' => ['en'],
    'GN' => ['fr'],
    'GP' => ['fr'],
    'GQ' => ['es', 'fr', 'pt'],
    'GR' => ['el'],
    'GS' => ['en'],
    'GT' => ['es'],
    'GU' => ['ch', 'en', 'es'],
    'GW' => ['pov', 'pt'],
    'GY' => ['en'],
    'HK' => ['en', 'zh'],
    'HM' => ['en'],
    'HN' => ['es'],
    'HR' => ['hr'],
    'HT' => ['fr', 'ht'],
    'HU' => ['hu'],
    'ID' => ['id'],
    'IE' => ['en', 'ga'],
    'IL' => ['ar', 'he'],
    'IM' => ['en', 'gv'],
    'IN' => ['en', 'hi', 'ta'],
    'IO' => ['en'],
    'IQ' => ['ar', 'arc', 'ckb', 'ku'],
    'IR' => ['fa'],
    'IS' => ['is'],
    'IT' => ['it'],
    'JE' => ['en', 'fr', 'nrf'],
    'JM' => ['en', 'jam'],
    'JO' => ['ar'],
    'JP' => ['ja'],
    'KE' => ['en', 'sw'],
    'KG' => ['ky', 'ru'],
    'KH' => ['km'],
    'KI' => ['en', 'gil'],
    'KM' => ['ar', 'fr', 'zdj'],
    'KN' => ['en'],
    'KP' => ['ko'],
    'KR' => ['ko'],
    'KW' => ['ar'],
    'KY' => ['en'],
    'KZ' => ['kk', 'ru'],
    'LA' => ['lo'],
    'LB' => ['ar', 'fr'],
    'LC' => ['en'],
    'LI' => ['de'],
    'LK' => ['si', 'ta'],
    'LR' => ['en'],
    'LS' => ['en', 'st'],
    'LT' => ['lt'],
    'LU' => ['de', 'fr', 'lb'],
    'LV' => ['lv'],
    'LY' => ['ar'],
    'MA' => ['ar', 'ber', 'fr'],
    'MC' => ['fr'],
    'MD' => ['ro'],
    'ME' => ['cnr', 'sr'],
    'MF' => ['fr'],
    'MG' => ['fr', 'mg'],
    'MH' => ['en', 'mh'],
    'MK' => ['mk'],
    'ML' => ['fr'],
    'MM' => ['my'],
    'MN' => ['mn'],
    'MO' => ['pt', 'zh'],
    'MP' => ['cal', 'ch', 'en'],
    'MQ' => ['fr'],
    'MR' => ['ar'],
    'MS' => ['en'],
    'MT' => ['en', 'mt'],
    'MU' => ['en', 'fr', 'mfe'],
    'MV' => ['dv'],
    'MW' => ['en', 'ny'],
    'MX' => ['es'],
    'MY' => ['en', 'ms'],
    'MZ' => ['pt'],
    'NA' => ['af', 'de', 'en', 'hgm', 'hz', 'kwn', 'loz', 'ng', 'tn'],
    'NC' => ['fr'],
    'NE' => ['fr'],
    'NF' => ['en', 'pih'],
    'NG' => ['en'],
    'NI' => ['es'],
    'NL' => ['nl'],
    'NO' => ['nb', 'nn', 'no', 'smi'],
    'NP' => ['ne'],
    'NR' => ['en', 'na'],
    'NU' => ['en', 'niu'],
    'NZ' => ['en', 'mi', 'nzs'],
    'OM' => ['ar'],
    'PA' => ['es'],
    'PE' => ['ay', 'es', 'qu'],
    'PF' => ['fr'],
    'PG' => ['en', 'ho', 'tpi'],
    'PH' => ['en', 'tl'],
    'PK' => ['en', 'ur'],
    'PL' => ['pl'],
    'PM' => ['fr'],
    'PN' => ['en'],
    'PR' => ['en', 'es'],
    'PS' => ['ar'],
    'PT' => ['pt'],
    'PW' => ['en', 'pau'],
    'PY' => ['es', 'gn'],
    'QA' => ['ar'],
    'RE' => ['fr'],
    'RO' => ['ro'],
    'RS' => ['sr'],
    'RU' => ['ru'],
    'RW' => ['en', 'fr', 'rw'],
    'SA' => ['ar'],
    'SB' => ['en'],
    'SC' => ['crs', 'en', 'fr'],
    'SD' => ['ar', 'en'],
    'SE' => ['sv'],
    'SG' => ['en', 'ms', 'ta', 'zh'],
    'SH' => ['en'],
    'SI' => ['sl'],
    'SJ' => ['no'],
    'SK' => ['sk'],
    'SL' => ['en'],
    'SM' => ['it'],
    'SN' => ['fr'],
    'SO' => ['ar', 'so'],
    'SR' => ['nl'],
    'SS' => ['en'],
    'ST' => ['pt'],
    'SV' => ['es'],
    'SX' => ['en', 'fr', 'nl'],
    'SY' => ['ar'],
    'SZ' => ['en', 'ss'],
    'TC' => ['en'],
    'TD' => ['ar', 'fr'],
    'TF' => ['fr'],
    'TG' => ['fr'],
    'TH' => ['th'],
    'TJ' => ['ru', 'tg'],
    'TK' => ['en', 'sm', 'tkl'],
    'TL' => ['pt', 'tet'],
    'TM' => ['ru', 'tk'],
    'TN' => ['ar', 'fr'],
    'TO' => ['en', 'to'],
    'TR' => ['tr'],
    'TT' => ['en'],
    'TV' => ['en', 'tvl'],
    'TW' => ['zh'],
    'TZ' => ['en', 'sw'],
    'UA' => ['ru', 'uk'],
    'UG' => ['en', 'sw'],
    'UM' => ['en'],
    'US' => ['en'],
    'UY' => ['es'],
    'UZ' => ['ru', 'uz'],
    'VA' => ['it', 'la'],
    'VC' => ['en'],
    'VE' => ['es'],
    'VG' => ['en'],
    'VI' => ['en'],
    'VN' => ['vi'],
    'VU' => ['bi', 'en', 'fr'],
    'WF' => ['fr'],
    'WS' => ['en', 'sm'],
    'XK' => ['sq', 'sr'],
    'YE' => ['ar'],
    'YT' => ['fr'],
    'ZA' => ['af', 'en', 'nr', 'nso', 'ss', 'st', 'tn', 'ts', 've', 'xh', 'zu'],
    'ZM' => ['en'],
    'ZW' => ['bwg', 'en', 'kck', 'khi', 'nd', 'ndc', 'ny', 'sn', 'st', 'tn', 'toi', 'ts', 've', 'xh', 'zib']    
];
