/* The Upper Room — i18n (Nova 2026-06-23). Tiny in-house translator.
 * Usage: T('auth.create_title')  ·  setLang('es')  ·  current via window.__UR_LANG
 * Lang resolves: profile (DB, set by app.jsx after /auth/me) -> localStorage -> 'en'.
 * Loaded BEFORE all other jsx so T() exists everywhere.
 */
(function () {
  const DICT = {
    en: {
      // gate / auth
      'auth.tagline': 'Where believers gather, work, and weigh it against the Word.',
      'auth.hero': 'Where believers',
      'auth.hero_em': 'gather.',
      'auth.create_sub': 'Join the gathering. Create your account — it only takes a moment.',
      'auth.signin_sub': 'Welcome back. Sign in to the gathering.',
      'auth.first': 'First name', 'auth.last': 'Last name',
      'auth.first_ph': 'First', 'auth.last_ph': 'Last',
      'auth.email': 'Email', 'auth.password': 'Password', 'auth.confirm': 'Confirm password',
      'auth.pw_choose': 'Choose a password', 'auth.pw_reenter': 'Re-enter your password', 'auth.pw_your': 'Your password',
      'auth.create_btn': 'Create account', 'auth.creating': 'Creating your account…',
      'auth.signin_btn': 'Sign in', 'auth.one_moment': 'One moment…',
      'auth.have_account': 'Already gathering?', 'auth.new_here': 'New here?',
      'auth.go_signin': 'Sign in', 'auth.go_create': 'Create an account',
      'auth.nomatch': 'The passwords don’t match.',
      'auth.fine': 'By continuing you agree to gather in good faith. We’ll never spam you.',
      'pw.len': 'At least 8 characters', 'pw.upper': 'An uppercase letter (A–Z)',
      'pw.lower': 'A lowercase letter (a–z)', 'pw.num': 'At least one number (0–9)',
      'pw.nosym': 'Letters and numbers only — no symbols',
      // nav
      'nav.home': 'Home', 'nav.circle': 'Circle', 'nav.gatherings': 'Gatherings',
      'nav.messages': 'Messages', 'nav.alerts': 'Alerts', 'nav.me': 'Me',
      'nav.search': 'Search the Upper Room',
      // left rail
      'rail.circle': 'Your circle', 'rail.saved': 'Saved words',
      'rail.gatherings': 'Gatherings', 'rail.prayer': 'Prayer Requests',
      // composer / feed
      'comp.share': 'Share a word with the room…', 'comp.share_locked': 'Complete your profile to share a word…',
      'comp.photo': 'Photo', 'comp.verse': 'Verse', 'comp.testimony': 'Testimony',
      'comp.word_tab': 'Word', 'comp.verse_tab': '✝ Verse', 'comp.testimony_tab': 'Testimony',
      'comp.verse_ph': 'Type a reference — e.g. John 3:16 or Psalm 23:1-3', 'comp.find': 'Find',
      'comp.note_verse': 'Add a word with this verse (optional)…',
      'comp.note_test': 'Give Him a shout — what did God do?',
      'comp.note_word': 'Share a word with the room…',
      'comp.photo_soon': 'Photo (soon)', 'comp.share_room': 'Share with the room', 'comp.sharing': 'Sharing…',
      'feed.caughtup': 'You’re all caught up. Take everything before the Holy Spirit. ✝',
      'post.amens': 'amens', 'post.amen': 'Amen', 'post.pray': 'Pray', 'post.comment': 'Comment', 'post.share': 'Share',
      'post.testimony_badge': '✨ Testimony',
      // profile setup
      'setup.step': 'Step 1 of 1 · Your profile', 'setup.signout': 'Not you? Sign out',
      'setup.title': 'Set up your community profile',
      'setup.intro': 'This isn’t a résumé — it’s how the room gets to know you. Tell us where you’re from and where you worship, and we’ll help you find believers near you.',
      'setup.where': 'Where are you from?', 'setup.where_ph': 'City, State',
      'setup.church': 'What church do you attend?', 'setup.church_ph': 'Your home church or fellowship',
      'setup.walk': 'Your walk', 'setup.walk_opt': '— optional',
      'setup.walk_ph': 'A sentence about your faith, your season, or what you’re believing God for right now.',
      'setup.skip': 'Skip — just looking', 'setup.enter': 'Enter the Room', 'setup.enter_busy': 'One moment…',
      'setup.photo': 'Your profile photo', 'setup.photo_hint': 'Optional — your initials work just fine for now.',
      'setup.upload': 'Upload a photo',
      // commons
      'commons.title': 'The Commons',
      'commons.blurb': 'Where the whole Upper Room gathers — to pray, share, and weigh it against the Word. No ads. No noise. Just believers.',
      'commons.spaces': 'Community spaces', 'commons.subtitle': 'Where the whole room gathers',
      'commons.all_spaces': 'All spaces',
      'commons.filter_all': 'All', 'commons.filter_conn': 'Connections', 'commons.filter_me': 'Me',
      'commons.share_with': 'Share with', 'commons.post': 'Post',
      // settings / language
      'set.language': 'Language', 'set.lang_en': 'English', 'set.lang_es': 'Español',
    },
    es: {
      'auth.tagline': 'Donde los creyentes se reúnen, trabajan y lo examinan a la luz de la Palabra.',
      'auth.hero': 'Donde los creyentes',
      'auth.hero_em': 'se reúnen.',
      'auth.create_sub': 'Únete a la reunión. Crea tu cuenta — solo toma un momento.',
      'auth.signin_sub': 'Bienvenido de nuevo. Inicia sesión en la reunión.',
      'auth.first': 'Nombre', 'auth.last': 'Apellido',
      'auth.first_ph': 'Nombre', 'auth.last_ph': 'Apellido',
      'auth.email': 'Correo', 'auth.password': 'Contraseña', 'auth.confirm': 'Confirmar contraseña',
      'auth.pw_choose': 'Elige una contraseña', 'auth.pw_reenter': 'Vuelve a escribir tu contraseña', 'auth.pw_your': 'Tu contraseña',
      'auth.create_btn': 'Crear cuenta', 'auth.creating': 'Creando tu cuenta…',
      'auth.signin_btn': 'Iniciar sesión', 'auth.one_moment': 'Un momento…',
      'auth.have_account': '¿Ya te reúnes?', 'auth.new_here': '¿Nuevo aquí?',
      'auth.go_signin': 'Inicia sesión', 'auth.go_create': 'Crea una cuenta',
      'auth.nomatch': 'Las contraseñas no coinciden.',
      'auth.fine': 'Al continuar aceptas reunirte de buena fe. Nunca te enviaremos spam.',
      'pw.len': 'Al menos 8 caracteres', 'pw.upper': 'Una letra mayúscula (A–Z)',
      'pw.lower': 'Una letra minúscula (a–z)', 'pw.num': 'Al menos un número (0–9)',
      'pw.nosym': 'Solo letras y números — sin símbolos',
      'nav.home': 'Inicio', 'nav.circle': 'Círculo', 'nav.gatherings': 'Reuniones',
      'nav.messages': 'Mensajes', 'nav.alerts': 'Avisos', 'nav.me': 'Yo',
      'nav.search': 'Buscar en el Aposento Alto',
      'rail.circle': 'Tu círculo', 'rail.saved': 'Palabras guardadas',
      'rail.gatherings': 'Reuniones', 'rail.prayer': 'Peticiones de oración',
      'comp.share': 'Comparte una palabra con la sala…', 'comp.share_locked': 'Completa tu perfil para compartir…',
      'comp.photo': 'Foto', 'comp.verse': 'Versículo', 'comp.testimony': 'Testimonio',
      'comp.word_tab': 'Palabra', 'comp.verse_tab': '✝ Versículo', 'comp.testimony_tab': 'Testimonio',
      'comp.verse_ph': 'Escribe una referencia — p. ej. Juan 3:16 o Salmo 23:1-3', 'comp.find': 'Buscar',
      'comp.note_verse': 'Añade una palabra con este versículo (opcional)…',
      'comp.note_test': 'Dale la gloria — ¿qué hizo Dios?',
      'comp.note_word': 'Comparte una palabra con la sala…',
      'comp.photo_soon': 'Foto (pronto)', 'comp.share_room': 'Compartir con la sala', 'comp.sharing': 'Compartiendo…',
      'feed.caughtup': 'Estás al día. Lleva todo ante el Espíritu Santo. ✝',
      'post.amens': 'amenes', 'post.amen': 'Amén', 'post.pray': 'Orar', 'post.comment': 'Comentar', 'post.share': 'Compartir',
      'post.testimony_badge': '✨ Testimonio',
      'setup.step': 'Paso 1 de 1 · Tu perfil', 'setup.signout': '¿No eres tú? Cerrar sesión',
      'setup.title': 'Configura tu perfil de la comunidad',
      'setup.intro': 'Esto no es un currículum — es cómo la sala te conoce. Cuéntanos de dónde eres y dónde congregas, y te ayudaremos a encontrar creyentes cerca de ti.',
      'setup.where': '¿De dónde eres?', 'setup.where_ph': 'Ciudad, Estado',
      'setup.church': '¿A qué iglesia asistes?', 'setup.church_ph': 'Tu iglesia o congregación',
      'setup.walk': 'Tu caminar', 'setup.walk_opt': '— opcional',
      'setup.walk_ph': 'Una frase sobre tu fe, tu temporada, o lo que le estás creyendo a Dios ahora mismo.',
      'setup.skip': 'Saltar — solo mirando', 'setup.enter': 'Entrar a la Sala', 'setup.enter_busy': 'Un momento…',
      'setup.photo': 'Tu foto de perfil', 'setup.photo_hint': 'Opcional — tus iniciales sirven por ahora.',
      'setup.upload': 'Subir una foto',
      'commons.title': 'La Sala Común',
      'commons.blurb': 'Donde se reúne todo el Aposento Alto — para orar, compartir y examinarlo a la luz de la Palabra. Sin anuncios. Sin ruido. Solo creyentes.',
      'commons.spaces': 'Espacios de la comunidad', 'commons.subtitle': 'Donde se reúne toda la sala',
      'commons.all_spaces': 'Todos los espacios',
      'commons.filter_all': 'Todo', 'commons.filter_conn': 'Conexiones', 'commons.filter_me': 'Yo',
      'commons.share_with': 'Comparte en', 'commons.post': 'Publicar',
      'set.language': 'Idioma', 'set.lang_en': 'English', 'set.lang_es': 'Español',
    },
  };

  function resolveLang() {
    try {
      if (window.__UR_LANG) return window.__UR_LANG;
      const ls = window.localStorage && localStorage.getItem('ur_lang');
      if (ls && DICT[ls]) return ls;
    } catch (e) {}
    return 'en';
  }
  window.__UR_LANG = resolveLang();

  window.T = function (key, fallback) {
    const lang = window.__UR_LANG || 'en';
    const d = DICT[lang] || DICT.en;
    if (d[key] != null) return d[key];
    if (DICT.en[key] != null) return DICT.en[key];
    return fallback != null ? fallback : key;
  };

  // set language: persist to localStorage immediately; app.jsx also POSTs /api/ur/lang for the profile.
  window.setLang = function (lang) {
    if (!DICT[lang]) return;
    window.__UR_LANG = lang;
    try { localStorage.setItem('ur_lang', lang); } catch (e) {}
    try {
      fetch('/api/ur/lang', { method: 'POST', credentials: 'same-origin', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ lang }) }).catch(() => {});
    } catch (e) {}
    // simplest reliable repaint across the babel-in-browser app: reload.
    try { window.location.reload(); } catch (e) {}
  };

  window.UR_LANGS = [['en', 'English'], ['es', 'Español']];
})();
