/**
 * "More languages" in the language menu: every other language Google Translate offers.
 * English and Spanish are real pages (src/i18n) — the rest is translated live in the browser by Google
 * (scripts/google-translate.ts). Names are built at build time: native name (Intl) + English name for search.
 */
import { LANG_CODES } from './index';

/** Google Translate language codes (website translator) */
const CODES = [
  'af', 'sq', 'am', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs', 'bg', 'ca', 'ceb', 'zh-CN', 'zh-TW', 'co', 'hr', 'cs', 'da', 'nl',
  'eo', 'et', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el', 'gu', 'ht', 'ha', 'haw', 'iw', 'hi', 'hmn', 'hu', 'is', 'ig', 'id', 'ga',
  'it', 'ja', 'jw', 'kn', 'kk', 'km', 'rw', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms', 'ml', 'mt', 'mi',
  'mr', 'mn', 'my', 'ne', 'no', 'ny', 'or', 'ps', 'fa', 'pl', 'pt', 'pa', 'ro', 'ru', 'sm', 'gd', 'sr', 'st', 'sn', 'sd', 'si',
  'sk', 'sl', 'so', 'su', 'sw', 'sv', 'tl', 'tg', 'ta', 'tt', 'te', 'th', 'tr', 'tk', 'uk', 'ur', 'ug', 'uz', 'vi', 'cy', 'xh',
  'yi', 'yo', 'zu',
] as const;

/** Google uses old codes for a few languages; Intl knows them by the new ones */
const INTL_CODE: Record<string, string> = { iw: 'he', jw: 'jv', 'zh-CN': 'zh-Hans', 'zh-TW': 'zh-Hant', tl: 'fil' };
/** shown first: languages many US truck drivers and owners speak */
export const POPULAR = ['pa', 'hi', 'ur', 'ru', 'uk', 'pt', 'fr', 'ar', 'vi', 'zh-CN', 'tl', 'pl', 'ro', 'bs', 'so', 'am', 'ht', 'ko'];
/** written right to left */
export const RTL = ['ar', 'iw', 'fa', 'ur', 'ps', 'sd', 'ug', 'yi', 'ku'];

const name = (locale: string, code: string) => {
  try { return new Intl.DisplayNames([locale], { type: 'language' }).of(code) || ''; } catch { return ''; }
};
const cap = (s: string) => s.charAt(0).toLocaleUpperCase() + s.slice(1);

export interface GoogleLang { code: string; native: string; english: string; popular: boolean; rtl: boolean }

/** every Google language except the ones the site has real pages for — popular first, then A–Z by English name */
export const GOOGLE_LANGS: GoogleLang[] = CODES
  .filter((c) => !(LANG_CODES as readonly string[]).includes(c))
  .map((code) => {
    const ic = INTL_CODE[code] || code;
    const english = name('en', ic) || code;
    const native = cap(name(ic, ic) || english);
    return { code, native, english, popular: POPULAR.includes(code), rtl: RTL.includes(code) };
  })
  .sort((a, b) => (a.popular === b.popular
    ? (a.popular ? POPULAR.indexOf(a.code) - POPULAR.indexOf(b.code) : a.english.localeCompare(b.english))
    : a.popular ? -1 : 1));
