import { en } from './en.js';
import { ru } from './ru.js';

// Use a structural type so languages with different keyboard layouts are assignable
export interface Messages {
  keyboardRows: readonly string[][];
  backspaceLabel: string;
  title: string;
  rules: {
    heading: string;
    bullets: readonly string[];
    greenDesc: string;
    yellowDesc: string;
    grayDesc: string;
    playButton: string;
  };
  game: {
    hint: string;
    unknownWord: string;
  };
  results: {
    congratulations: string;
    betterLuck: string;
    solvedIn: (n: number) => string;
    theWordWas: (w: string) => string;
    statistics: string;
    played: string;
    winRate: string;
    streak: string;
    maxStreak: string;
    noGames: string;
    solverCaption: string;
    playAgain: string;
  };
  keyEnter: string;
  keyBackspace: string;
}

/** Detect language from URL path. /ru/... → 'ru', otherwise 'en'. */
export function detectLang(): 'en' | 'ru' {
  if (window.location.pathname.startsWith('/ru')) return 'ru';
  return 'en';
}

const MESSAGES: Record<string, Messages> = { en, ru };

export function getMessages(): Messages {
  return MESSAGES[detectLang()] ?? en;
}

/** API base path including language prefix. /ru/... → '/ru/api', otherwise '/api'. */
export function getApiBase(): string {
  return detectLang() === 'ru' ? '/ru/api' : '/api';
}
