export type TranslationKeys = ReturnType<typeof getStrings>;

export function en() {
  return {
    appTitle: "Quibot Control",
    settingsTitle: "Settings",
    back: "Back",
    save: "Save",
    backendUrl: "Backend URL",
    bearerToken: "Bearer token",
    formFieldName: "Form field name",
    tokenOptional: "Optional",
    fieldNamePlaceholder: "file",
    urlPlaceholder: "https://api.example.com/upload",
    helperText: `The recording is uploaded as multipart field '{field}'.`,
    savedAlert: "Settings saved.",
    loadError: "Could not load backend settings.",
    saveError: "Could not save backend settings.",
    languageTitle: "Language",
    recorderTitle: "Voice recorder",
    readyToRecord: "Ready to record.",
    recording: "Recording...",
    micPermissionDenied: "Microphone permission was denied.",
    micAccessRequiredTitle: "Microphone access required",
    micAccessRequiredMsg: "Enable microphone access to record audio.",
    couldNotStartRecording: "Could not start recording.",
    recordingFailedTitle: "Recording failed",
    finishedUpload: "Recording finished. Uploading...",
    voiceMessageSent: "Voice message sent.",
    uploadFailed: "Upload failed.",
    noBackendUrl: "Recording finished. No backend URL set.",
    stopFailedTitle: "Stop failed",
    missingBackendUrlTitle: "Missing backend URL",
    missingBackendUrlMsg: "Enter the backend endpoint first.",
    uploadingRecording: "Uploading recording.",
    uploadComplete: "Upload complete.",
    serverResponse: "Server response",
    releaseToStop: "Release your finger to stop recording and send the audio.",
    holdToRecord: "Hold the microphone button to record a voice message. Release to send it immediately.",
    openSettingsHint: "Open settings to add your backend URL before sending voice messages.",
     aiReply: "Quibot reply",
     yourMessage: "Your message",
    playing: "Playing audio...",
  };
}

export function ca() {
  return {
    appTitle: "Quibot Control",
    settingsTitle: "Configuraci\u00f3",
    back: "Enrere",
    save: "Desa",
    backendUrl: "URL del servidor",
    bearerToken: "Bearer token",
    formFieldName: "Nom del camp del formulari",
    tokenOptional: "Opcional",
    fieldNamePlaceholder: "file",
    urlPlaceholder: "https://api.example.com/upload",
    helperText: `La gravaci\u00f3 es penja com el camp multipart '{field}'.`,
    savedAlert: "Configuraci\u00f3 desada.",
    loadError: "No s'han pogut carregar les configuracions.",
    saveError: "No s'han pogut desar les configuracions.",
    languageTitle: "Llenguatge",
    recorderTitle: "Enregistrador de veu",
    readyToRecord: "Preparat per enregistrar.",
    recording: "Enregistrant...",
    micPermissionDenied: "S'ha denegat el perm\u00eds del micr\u00f2fon.",
    micAccessRequiredTitle: "Acc\u00e9s al micr\u00f2fon necess\u00e0ri",
    micAccessRequiredMsg: "Activa l'acc\u00e9s al micr\u00f2fon per enregistrar \u00e0udio.",
    couldNotStartRecording: "No s'ha pogut iniciar l'enregistrament.",
    recordingFailedTitle: "L'enregistrament ha fallat",
    finishedUpload: "Gravaci\u00f3 finalitzada. S'est\u00e0 penjant...",
    voiceMessageSent: "Missatge de veu enviat.",
    uploadFailed: "No s'ha pogut penjar.",
    noBackendUrl: "Gravaci\u00f3 finalitzada. No hi ha URL configurada.",
    stopFailedTitle: "S'ha aturat",
    missingBackendUrlTitle: "Falta la URL del servidor",
    missingBackendUrlMsg: "Introdueix primer l'URL del servidor.",
    uploadingRecording: "S'est\u00e0 penjant la gravaci\u00f3.",
    uploadComplete: "Penjada completada.",
    serverResponse: "Resposta del servidor",
    releaseToStop: "Allibera el dit per aturar l'enregistrament i enviar l'\u00e0udio.",
    holdToRecord: "Mant\u00e9s premut el micr\u00f2fon per enregistrar un missatge de veu. Allibera'l per enviar-lo immediatament.",
    openSettingsHint: "Obre la configuraci\u00f3 per afegir l'URL del servidor abans d'enviar missatges de veu.",
     aiReply: "Resposta del Quibot",
    yourMessage: "El teu missatge",
    playing: "Reproduint àudio...",
  };
}

const translations = { en, ca };
export type Locale = keyof typeof translations;
const DEFAULT_LOCALE: Locale = "ca";
export const AVAILABLE_LOCALES: readonly Locale[] = Object.keys(translations) as Locale[];

export function getStrings(locale: Locale) {
  const fn = translations[locale] ?? en;
  return fn();
}

export function t(key: keyof ReturnType<typeof ca>, locale: Locale, field?: string) {
  const strings = getStrings(locale);
  const value = strings[key as keyof typeof strings];
  if (typeof value !== "string") return String(value);
  return field ? value.replace("{field}", field) : value;
}
