import { formatLocale, formatPath } from "../chokidar/dist/esm/utils/formatter.mjs";
import { writeContentDeclaration } from "../chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs";
import { getDictionaries } from "../dictionaries-entry/dist/esm/index.mjs";
import { formatFillData } from "./formatFillData.mjs";
import { relative } from "node:path";
import { colorizeKey, getAppLogger } from "@intlayer/config";

//#region ../@intlayer/cli/dist/esm/fill/writeFill.mjs
const writeFill = async (contentDeclarationFile, outputLocales, parentLocales, configuration) => {
	const appLogger = getAppLogger(configuration);
	const fullDictionary = getDictionaries(configuration)[contentDeclarationFile.key];
	const { filePath } = contentDeclarationFile;
	if (!filePath) {
		appLogger("No file path found for dictionary", { level: "error" });
		return;
	}
	const autoFillOptions = contentDeclarationFile.fill ?? configuration.dictionary?.fill ?? false;
	if (typeof autoFillOptions === "boolean" && autoFillOptions === false) {
		appLogger(`Auto fill is disabled for '${colorizeKey(fullDictionary.key)}'`, { level: "info" });
		return;
	}
	const autoFillData = formatFillData(autoFillOptions, (outputLocales ?? configuration.internationalization.locales).filter((locale) => !parentLocales?.includes(locale)), filePath, fullDictionary.key, configuration);
	for await (const output of autoFillData) {
		if (!output.filePath) {
			appLogger(`No file path found for auto filled content declaration for '${colorizeKey(fullDictionary.key)}'`, { level: "error" });
			continue;
		}
		const { fill,...rest } = contentDeclarationFile;
		const relativeFilePath = relative(configuration.content.baseDir, output.filePath);
		await writeContentDeclaration({
			...rest,
			filled: true,
			locale: output.isPerLocale ? output.localeList[0] : void 0,
			localId: `${contentDeclarationFile.key}::local::${relativeFilePath}`,
			filePath: relativeFilePath
		}, configuration, { localeList: output.localeList });
		if (output.isPerLocale) {
			const sourceLocale = output.localeList[0];
			appLogger(`Auto filled per-locale content declaration for '${colorizeKey(fullDictionary.key)}' written to ${formatPath(output.filePath)} for locale ${formatLocale(sourceLocale)}`, { level: "info" });
		} else appLogger(`Auto filled content declaration for '${colorizeKey(fullDictionary.key)}' written to ${formatPath(output.filePath)}`, { level: "info" });
	}
};

//#endregion
export { writeFill };
//# sourceMappingURL=writeFill.mjs.map