import { logger } from "../../../logger/index.mjs";
import { AIProvider } from "../aiSdk.mjs";
import { readAsset } from "../../../_virtual/_utils_asset.mjs";
import { extractJson } from "../../extractJSON.mjs";
import { Locales } from "@intlayer/types";
import { getLocaleName } from "@intlayer/core";
import { generateText } from "ai";

//#region src/utils/AI/translateJSON/index.ts
const CHAT_GPT_PROMPT = readAsset("./PROMPT.md");
const aiDefaultOptions = {
	provider: AIProvider.OPENAI,
	model: "gpt-5-mini"
};
/**
* Format a locale with its name.
*
* @param locale - The locale to format.
* @returns A string in the format "locale: name", e.g. "en: English".
*/
const formatLocaleWithName = (locale) => `${locale}: ${getLocaleName(locale, Locales.ENGLISH)}`;
/**
* Formats tag instructions for the AI prompt.
* Creates a string with all available tags and their descriptions.
*
* @param tags - The list of tags to format.
* @returns A formatted string with tag instructions.
*/
const formatTagInstructions = (tags) => {
	if (!tags || tags.length === 0) return "";
	return `Based on the dictionary content, identify specific tags from the list below that would be relevant:
  
${tags.map(({ key, description }) => `- ${key}: ${description}`).join("\n\n")}`;
};
const getModeInstructions = (mode) => {
	if (mode === "complete") return "Mode: \"Complete\" - Enrich the preset content with the missing keys and values in the output locale. Do not update existing keys. Everything should be returned in the output.";
	return "Mode: \"Review\" - Fill missing content and review existing keys from the preset content. If a key from the entry is missing in the output, it must be translated to the target language and added. If you detect misspelled content, or content that should be reformulated, correct it. If a translation is not coherent with the desired language, translate it.";
};
/**
* TranslateJSONs a content declaration file by constructing a prompt for AI models.
* The prompt includes details about the project's locales, file paths of content declarations,
* and requests for identifying issues or inconsistencies.
*/
const translateJSON = async ({ entryFileContent, presetOutputContent, dictionaryDescription, aiConfig, entryLocale, outputLocale, tags, mode, applicationContext }) => {
	const prompt = CHAT_GPT_PROMPT.replace("{{entryLocale}}", formatLocaleWithName(entryLocale)).replace("{{outputLocale}}", formatLocaleWithName(outputLocale)).replace("{{entryFileContent}}", JSON.stringify(entryFileContent)).replace("{{presetOutputContent}}", JSON.stringify(presetOutputContent)).replace("{{dictionaryDescription}}", dictionaryDescription ?? "").replace("{{applicationContext}}", applicationContext ?? "").replace("{{tagsInstructions}}", formatTagInstructions(tags)).replace("{{modeInstructions}}", getModeInstructions(mode));
	const { text: newContent, usage } = await generateText({
		...aiConfig,
		messages: [{
			role: "system",
			content: prompt
		}]
	});
	logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);
	return {
		fileContent: extractJson(newContent),
		tokenUsed: usage?.totalTokens ?? 0
	};
};

//#endregion
export { aiDefaultOptions, translateJSON };
//# sourceMappingURL=index.mjs.map