/**
 * EN→RU keyboard layout converter.
 *
 * TZ §4.1.9.1: if no city/airport match is found for the typed query, the
 * system must retry with the query re-interpreted as if the user typed it
 * with the Russian keyboard layout active but the EN layout was selected
 * (e.g. "vjc" → "мос").
 *
 * Ported from the Angular reference:
 * ClientApp/src/app/shared/helpers/keyboardLayoutConverter.ts
 * (which itself was adapted from https://github.com/ai/convert-layout)
 */

function buildConverter(keys: string, values: string) {
  const full: Record<string, string> = {};

  for (let i = keys.length; i--; ) {
    const k = keys[i];
    const v = values[i];
    if (k === undefined || v === undefined) continue;
    full[k.toUpperCase()] = v.toUpperCase();
    full[k] = v;
  }

  return {
    fromEn(str: string): string {
      return str.replace(/./g, (ch) => full[ch] ?? ch);
    },
  };
}

// Copied from the Angular build of convert-layout (ruKeyboardLayout).
export const ruKeyboardLayout = buildConverter(
  '.exportsfunc"#$&\',/:;<>?@[]^`abdghijklmqvwyz{|}~',
  'юучзщкеыагтсЭ№;?эб.ЖжБЮ,"хъ:ёфивпршолдьймцняХ/ЪЁ',
);
