// i18n.jsx — translations + auto language detection
// Exposes window.I18N (dictionaries), window.detectLang(), window.LANGS (selector list)

const I18N = {
  tr: {
    label: "Türkçe",
    heroTitle: "Promptunu daha iyi hale getir",
    heroSub: "Fikrini yaz, gönder. Seçtiğin modele göre daha net, daha güçlü bir prompt'a dönüştürelim.",
    placeholder: "Promptunu buraya yaz… Örn: bir kahve markası için instagram açıklaması yaz",
    target: "Hedef model",
    general: "Genel",
    modeText: "Metin",
    modeImage: "Görsel",
    modeVideo: "Video",
    modeCode: "Kod",
    modeTextHint: "Sohbet / yazma promptu",
    modeImageHint: "Görsel oluşturma promptu",
    modeVideoHint: "Video oluşturma promptu",
    modeCodeHint: "Kod / teknik prompt",
    optimize: "Optimize et",
    optimizing: "Optimize ediliyor…",
    resultTitle: "Optimize edilmiş prompt",
    copy: "Kopyala",
    copied: "Kopyalandı",
    regenerate: "Yeniden",
    reoptimize: "Şu modele özel optimize et",
    history: "Geçmiş",
    historyEmpty: "Henüz geçmiş yok. İlk prompt'unu optimize et.",
    clear: "Temizle",
    newChat: "Yeni",
    you: "Senin draft'ın",
    error: "Bir şeyler ters gitti. Lütfen tekrar dene.",
    emptyWarn: "Önce bir prompt yaz.",
    light: "Açık",
    dark: "Koyu",
    chars: "karakter",
    optimizedFor: "için optimize edildi",
    selectLang: "Dil",
    settings: "Ayarlar",
    apiKeyTitle: "Gemini API anahtarı",
    apiKeyDesc: "Çevrimdışı sürümde optimizasyonu çalıştırmak için kendi Gemini API anahtarını gir.",
    apiKeyPlaceholder: "API anahtarını yapıştır…",
    apiKeyGet: "Ücretsiz anahtar al",
    apiKeyNote: "Anahtar yalnızca bu tarayıcıda saklanır; doğrudan Google'a gönderilir, başka hiçbir yere gitmez.",
    apiKeySave: "Kaydet",
    apiKeyClear: "Sil",
    apiKeyActive: "Bağlandı",
    apiKeyBuiltin: "Yerleşik AI etkin — anahtar isteğe bağlı.",
    noKeyError: "Optimize etmek için bir Gemini API anahtarı ekle.",
  },
  en: {
    label: "English",
    heroTitle: "Make your prompt better",
    heroSub: "Write your idea and send it. We'll turn it into a sharper, stronger prompt tuned to your model.",
    placeholder: "Write your prompt here… e.g. write an instagram caption for a coffee brand",
    target: "Target model",
    general: "General",
    modeText: "Text",
    modeImage: "Image",
    modeVideo: "Video",
    modeCode: "Code",
    modeTextHint: "Chat / writing prompt",
    modeImageHint: "Image generation prompt",
    modeVideoHint: "Video generation prompt",
    modeCodeHint: "Code / technical prompt",
    optimize: "Optimize",
    optimizing: "Optimizing…",
    resultTitle: "Optimized prompt",
    copy: "Copy",
    copied: "Copied",
    regenerate: "Regenerate",
    reoptimize: "Optimize for a specific model",
    history: "History",
    historyEmpty: "No history yet. Optimize your first prompt.",
    clear: "Clear",
    newChat: "New",
    you: "Your draft",
    error: "Something went wrong. Please try again.",
    emptyWarn: "Write a prompt first.",
    light: "Light",
    dark: "Dark",
    chars: "characters",
    optimizedFor: "optimized for",
    selectLang: "Language",
    settings: "Settings",
    apiKeyTitle: "Gemini API key",
    apiKeyDesc: "Enter your own Gemini API key to run optimization in the offline version.",
    apiKeyPlaceholder: "Paste your API key…",
    apiKeyGet: "Get a free key",
    apiKeyNote: "The key is stored only in this browser and sent directly to Google — nowhere else.",
    apiKeySave: "Save",
    apiKeyClear: "Clear",
    apiKeyActive: "Connected",
    apiKeyBuiltin: "Built-in AI active — key optional.",
    noKeyError: "Add a Gemini API key to optimize.",
  },
  es: {
    label: "Español",
    heroTitle: "Mejora tu prompt",
    heroSub: "Escribe tu idea y envíala. La convertimos en un prompt más claro y potente, ajustado a tu modelo.",
    placeholder: "Escribe tu prompt aquí… p. ej. escribe un pie de foto de instagram para una marca de café",
    target: "Modelo objetivo",
    general: "General",
    modeText: "Texto",
    modeImage: "Imagen",
    modeVideo: "Vídeo",
    modeCode: "Código",
    modeTextHint: "Prompt de chat / escritura",
    modeImageHint: "Prompt de generación de imagen",
    modeVideoHint: "Prompt de generación de vídeo",
    modeCodeHint: "Prompt de código / técnico",
    optimize: "Optimizar",
    optimizing: "Optimizando…",
    resultTitle: "Prompt optimizado",
    copy: "Copiar",
    copied: "Copiado",
    regenerate: "Regenerar",
    reoptimize: "Optimizar para un modelo específico",
    history: "Historial",
    historyEmpty: "Aún no hay historial. Optimiza tu primer prompt.",
    clear: "Borrar",
    newChat: "Nuevo",
    you: "Tu borrador",
    error: "Algo salió mal. Inténtalo de nuevo.",
    emptyWarn: "Escribe un prompt primero.",
    light: "Claro",
    dark: "Oscuro",
    chars: "caracteres",
    optimizedFor: "optimizado para",
    selectLang: "Idioma",
    settings: "Ajustes",
    apiKeyTitle: "Clave API de Gemini",
    apiKeyDesc: "Introduce tu propia clave API de Gemini para optimizar en la versión sin conexión.",
    apiKeyPlaceholder: "Pega tu clave API…",
    apiKeyGet: "Consigue una clave gratis",
    apiKeyNote: "La clave se guarda solo en este navegador y se envía directamente a Google, a ningún otro sitio.",
    apiKeySave: "Guardar",
    apiKeyClear: "Borrar",
    apiKeyActive: "Conectado",
    apiKeyBuiltin: "IA integrada activa — clave opcional.",
    noKeyError: "Añade una clave API de Gemini para optimizar.",
  },
  de: {
    label: "Deutsch",
    heroTitle: "Mach deinen Prompt besser",
    heroSub: "Schreib deine Idee und sende sie. Wir machen daraus einen klareren, stärkeren Prompt für dein Modell.",
    placeholder: "Schreib deinen Prompt hier… z. B. schreibe eine Instagram-Caption für eine Kaffeemarke",
    target: "Zielmodell",
    general: "Allgemein",
    modeText: "Text",
    modeImage: "Bild",
    modeVideo: "Video",
    modeCode: "Code",
    modeTextHint: "Chat- / Schreib-Prompt",
    modeImageHint: "Bildgenerierungs-Prompt",
    modeVideoHint: "Videogenerierungs-Prompt",
    modeCodeHint: "Code- / technischer Prompt",
    optimize: "Optimieren",
    optimizing: "Wird optimiert…",
    resultTitle: "Optimierter Prompt",
    copy: "Kopieren",
    copied: "Kopiert",
    regenerate: "Neu",
    reoptimize: "Für ein bestimmtes Modell optimieren",
    history: "Verlauf",
    historyEmpty: "Noch kein Verlauf. Optimiere deinen ersten Prompt.",
    clear: "Leeren",
    newChat: "Neu",
    you: "Dein Entwurf",
    error: "Etwas ist schiefgelaufen. Bitte erneut versuchen.",
    emptyWarn: "Schreib zuerst einen Prompt.",
    light: "Hell",
    dark: "Dunkel",
    chars: "Zeichen",
    optimizedFor: "optimiert für",
    selectLang: "Sprache",
    settings: "Einstellungen",
    apiKeyTitle: "Gemini-API-Schlüssel",
    apiKeyDesc: "Gib deinen eigenen Gemini-API-Schlüssel ein, um die Optimierung in der Offline-Version zu nutzen.",
    apiKeyPlaceholder: "API-Schlüssel einfügen…",
    apiKeyGet: "Kostenlosen Schlüssel holen",
    apiKeyNote: "Der Schlüssel wird nur in diesem Browser gespeichert und direkt an Google gesendet — sonst nirgendwo.",
    apiKeySave: "Speichern",
    apiKeyClear: "Löschen",
    apiKeyActive: "Verbunden",
    apiKeyBuiltin: "Integrierte KI aktiv — Schlüssel optional.",
    noKeyError: "Füge einen Gemini-API-Schlüssel hinzu, um zu optimieren.",
  },
  fr: {
    label: "Français",
    heroTitle: "Améliore ton prompt",
    heroSub: "Écris ton idée et envoie-la. On en fait un prompt plus clair et plus efficace, adapté à ton modèle.",
    placeholder: "Écris ton prompt ici… ex. écris une légende instagram pour une marque de café",
    target: "Modèle cible",
    general: "Général",
    modeText: "Texte",
    modeImage: "Image",
    modeVideo: "Vidéo",
    modeCode: "Code",
    modeTextHint: "Prompt de chat / rédaction",
    modeImageHint: "Prompt de génération d'image",
    modeVideoHint: "Prompt de génération de vidéo",
    modeCodeHint: "Prompt de code / technique",
    optimize: "Optimiser",
    optimizing: "Optimisation…",
    resultTitle: "Prompt optimisé",
    copy: "Copier",
    copied: "Copié",
    regenerate: "Régénérer",
    reoptimize: "Optimiser pour un modèle précis",
    history: "Historique",
    historyEmpty: "Pas encore d'historique. Optimise ton premier prompt.",
    clear: "Effacer",
    newChat: "Nouveau",
    you: "Ton brouillon",
    error: "Une erreur s'est produite. Réessaie.",
    emptyWarn: "Écris d'abord un prompt.",
    light: "Clair",
    dark: "Sombre",
    chars: "caractères",
    optimizedFor: "optimisé pour",
    selectLang: "Langue",
    settings: "Paramètres",
    apiKeyTitle: "Clé API Gemini",
    apiKeyDesc: "Saisis ta propre clé API Gemini pour lancer l'optimisation dans la version hors ligne.",
    apiKeyPlaceholder: "Colle ta clé API…",
    apiKeyGet: "Obtenir une clé gratuite",
    apiKeyNote: "La clé est stockée uniquement dans ce navigateur et envoyée directement à Google — nulle part ailleurs.",
    apiKeySave: "Enregistrer",
    apiKeyClear: "Effacer",
    apiKeyActive: "Connecté",
    apiKeyBuiltin: "IA intégrée active — clé facultative.",
    noKeyError: "Ajoute une clé API Gemini pour optimiser.",
  },
};

const LANGS = ["tr", "en", "es", "de", "fr"];

function detectLang() {
  try {
    const saved = localStorage.getItem("bp_lang");
    if (saved && I18N[saved]) return saved;
    const navs = navigator.languages || [navigator.language || "en"];
    for (const n of navs) {
      const code = (n || "").slice(0, 2).toLowerCase();
      if (I18N[code]) return code;
    }
  } catch (e) {}
  return "en";
}

Object.assign(window, { I18N, LANGS, detectLang });
