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

//#region src/utils/AI/auditDictionary/index.ts
const CHAT_GPT_PROMPT = readAsset("./PROMPT.md");
const aiDefaultOptions = {};
/**
* 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) => {
	return `${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")].join("\n\n");
};
/**
* Audits 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 auditDictionary = async ({ fileContent, filePath, locales, defaultLocale, tags, aiConfig, applicationContext }) => {
	const otherLocales = locales.filter((locale) => locale !== defaultLocale);
	const prompt = CHAT_GPT_PROMPT.replace("{{defaultLocale}}", formatLocaleWithName(defaultLocale)).replace("{{otherLocales}}", `{${otherLocales.map(formatLocaleWithName).join(", ")}}`).replace("{{filePath}}", filePath ?? "").replace("{{applicationContext}}", applicationContext ?? "").replace("{{tagsInstructions}}", formatTagInstructions(tags));
	const { text: newContent, usage } = await generateText({
		...aiConfig,
		messages: [{
			role: "system",
			content: prompt
		}, {
			role: "user",
			content: ["**File to Audit:**", fileContent].join("\n")
		}]
	});
	logger.info(`${usage?.totalTokens ?? 0} tokens used in the request`);
	return {
		fileContent: extractJson(newContent),
		tokenUsed: usage?.totalTokens ?? 0
	};
};

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