const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_fill_formatFillData = require('./formatFillData.cjs');
let __intlayer_chokidar = require("@intlayer/chokidar");
let __intlayer_config = require("@intlayer/config");
let node_path = require("node:path");
let __intlayer_dictionaries_entry = require("@intlayer/dictionaries-entry");

//#region src/fill/writeFill.ts
const writeFill = async (contentDeclarationFile, outputLocales, parentLocales, configuration) => {
	const appLogger = (0, __intlayer_config.getAppLogger)(configuration);
	const fullDictionary = (0, __intlayer_dictionaries_entry.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 '${(0, __intlayer_config.colorizeKey)(fullDictionary.key)}'`, { level: "info" });
		return;
	}
	const autoFillData = require_fill_formatFillData.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 '${(0, __intlayer_config.colorizeKey)(fullDictionary.key)}'`, { level: "error" });
			continue;
		}
		const { fill,...rest } = contentDeclarationFile;
		const relativeFilePath = (0, node_path.relative)(configuration.content.baseDir, output.filePath);
		await (0, __intlayer_chokidar.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 '${(0, __intlayer_config.colorizeKey)(fullDictionary.key)}' written to ${(0, __intlayer_chokidar.formatPath)(output.filePath)} for locale ${(0, __intlayer_chokidar.formatLocale)(sourceLocale)}`, { level: "info" });
		} else appLogger(`Auto filled content declaration for '${(0, __intlayer_config.colorizeKey)(fullDictionary.key)}' written to ${(0, __intlayer_chokidar.formatPath)(output.filePath)}`, { level: "info" });
	}
};

//#endregion
exports.writeFill = writeFill;
//# sourceMappingURL=writeFill.cjs.map