import { useWikiStore } from "@/stores/wiki-store"
import { detectLanguage } from "./detect-language"
import { getLanguagePromptName } from "./language-metadata"

/**
 * Get the effective output language for LLM content generation.
 *
 * If user has explicitly set an outputLanguage, use it.
 * Otherwise (auto), fall back to detecting the language from the given text.
 */
export function getOutputLanguage(fallbackText: string = ""): string {
  const configured = useWikiStore.getState().outputLanguage
  if (configured && configured !== "auto") {
    return configured
  }
  return detectLanguage(fallbackText || "English")
}

/**
 * Build a strong language directive to inject into system prompts.
 */
export function buildLanguageDirective(fallbackText: string = ""): string {
  const lang = getOutputLanguage(fallbackText)
  const promptLang = getLanguagePromptName(lang)
  return [
    `## ⚠️ MANDATORY OUTPUT LANGUAGE: ${promptLang}`,
    "",
    `Write surrounding natural-language prose in **${promptLang}**.`,
    `All generated prose, including prose titles and section headings, must be in ${promptLang}.`,
    `Do not translate, transliterate, or describe proper nouns and technical identifiers unless the source already uses a well-established localized form.`,
    `Preserve organization names, product names, model names, dataset names, tool/library names, acronyms, code identifiers, file names, URLs, paper titles, citation strings, and technical terms that have no widely-used localized equivalent in their standard original form.`,
    `The source material or wiki content may be in a different language; use it as evidence, but keep generated prose in ${promptLang}.`,
    `This language rule overrides weaker style instructions, but it does not override the proper-noun and technical-identifier preservation rule above.`,
  ].join("\n")
}

/**
 * Short reminder version — for placing right before user's current message.
 */
export function buildLanguageReminder(fallbackText: string = ""): string {
  const lang = getOutputLanguage(fallbackText)
  return `REMINDER: Write prose in ${getLanguagePromptName(lang)}; preserve names, acronyms, identifiers, URLs, file names, and paper titles in their standard original form.`
}
