// Countries where Feeld is NOT available.
// Source: https://support.feeld.co/hc/en-gb/articles/14876019037212-Countries-where-Feeld-is-NOT-available
//
// Using a location in any of these countries will get the account flagged inactive
// per Feeld's policy. The web client refuses to set/save them.

export interface BannedCountry {
  name: string;
  code: string; // ISO 3166-1 alpha-2
  aliases?: string[];
}

export const BANNED_COUNTRIES: BannedCountry[] = [
  { name: 'Albania', code: 'AL' },
  { name: 'Algeria', code: 'DZ' },
  { name: 'Angola', code: 'AO' },
  { name: 'Armenia', code: 'AM' },
  { name: 'Azerbaijan', code: 'AZ' },
  { name: 'Bahrain', code: 'BH' },
  { name: 'Bangladesh', code: 'BD' },
  { name: 'Benin', code: 'BJ' },
  { name: 'Botswana', code: 'BW' },
  { name: 'Brunei Darussalam', code: 'BN', aliases: ['Brunei'] },
  { name: 'Burkina Faso', code: 'BF' },
  { name: 'Cameroon', code: 'CM' },
  { name: 'Chad', code: 'TD' },
  { name: 'China', code: 'CN', aliases: ["People's Republic of China", 'PRC'] },
  { name: 'Republic of the Congo', code: 'CG', aliases: ['Congo', 'Congo, Republic of', 'Congo-Brazzaville'] },
  { name: 'Cuba', code: 'CU' },
  { name: 'Egypt', code: 'EG' },
  { name: 'Gabon', code: 'GA' },
  { name: 'Gambia', code: 'GM', aliases: ['The Gambia'] },
  { name: 'Hong Kong', code: 'HK' },
  { name: 'Iran', code: 'IR', aliases: ['Islamic Republic of Iran'] },
  { name: 'Iraq', code: 'IQ' },
  { name: 'Jordan', code: 'JO' },
  { name: 'Kazakhstan', code: 'KZ' },
  { name: 'Kenya', code: 'KE' },
  { name: 'Laos', code: 'LA', aliases: ["Lao People's Democratic Republic", 'Lao PDR'] },
  { name: 'Liberia', code: 'LR' },
  { name: 'Macau', code: 'MO', aliases: ['Macao'] },
  { name: 'Madagascar', code: 'MG' },
  { name: 'Malawi', code: 'MW' },
  { name: 'Mali', code: 'ML' },
  { name: 'Mauritania', code: 'MR' },
  { name: 'Mauritius', code: 'MU' },
  { name: 'Myanmar', code: 'MM', aliases: ['Burma', 'Mayanmar'] }, // "Mayanmar" is Feeld's page typo
  { name: 'Morocco', code: 'MA' },
  { name: 'Mozambique', code: 'MZ' },
  { name: 'Namibia', code: 'NA' },
  { name: 'Niger', code: 'NE' },
  { name: 'Oman', code: 'OM' },
  { name: 'Pakistan', code: 'PK' },
  { name: 'Papua New Guinea', code: 'PG' },
  { name: 'Philippines', code: 'PH' },
  { name: 'Qatar', code: 'QA' },
  { name: 'Russia', code: 'RU', aliases: ['Russian Federation'] },
  { name: 'Saudi Arabia', code: 'SA' },
  { name: 'Senegal', code: 'SN' },
  { name: 'Sierra Leone', code: 'SL' },
  { name: 'Sudan', code: 'SD' },
  { name: 'Taiwan', code: 'TW', aliases: ['Taiwan, Province of China', 'Republic of China'] },
  { name: 'Tajikistan', code: 'TJ' },
  { name: 'Tanzania', code: 'TZ', aliases: ['United Republic of Tanzania'] },
  { name: 'Thailand', code: 'TH' },
  { name: 'Togo', code: 'TG' },
  { name: 'Tunisia', code: 'TN' },
  { name: 'Turkmenistan', code: 'TM' },
  { name: 'Uganda', code: 'UG' },
  { name: 'Uzbekistan', code: 'UZ' },
  { name: 'Yemen', code: 'YE' },
  { name: 'Zambia', code: 'ZM' },
  { name: 'Zimbabwe', code: 'ZW' },
];

const BANNED_CODES = new Set(BANNED_COUNTRIES.map((c) => c.code.toUpperCase()));
const BANNED_NAMES = new Set<string>();
for (const c of BANNED_COUNTRIES) {
  BANNED_NAMES.add(c.name.toLowerCase());
  for (const alias of c.aliases ?? []) BANNED_NAMES.add(alias.toLowerCase());
}

const normalize = (s: string) => s.trim().toLowerCase();

export function isCountryBanned(country?: string | null, countryCode?: string | null): boolean {
  if (countryCode && BANNED_CODES.has(countryCode.trim().toUpperCase())) return true;
  if (country && BANNED_NAMES.has(normalize(country))) return true;
  return false;
}

export function findBannedCountry(country?: string | null, countryCode?: string | null): BannedCountry | null {
  if (countryCode) {
    const code = countryCode.trim().toUpperCase();
    const hit = BANNED_COUNTRIES.find((c) => c.code === code);
    if (hit) return hit;
  }
  if (country) {
    const n = normalize(country);
    const hit = BANNED_COUNTRIES.find(
      (c) => c.name.toLowerCase() === n || c.aliases?.some((a) => a.toLowerCase() === n)
    );
    if (hit) return hit;
  }
  return null;
}
