const require_rolldown_runtime = require('../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../config/dist/esm/logger.cjs');
const require_formatter = require('../chokidar/dist/esm/utils/formatter.cjs');
const require_writeContentDeclaration = require('../chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.cjs');
const require_index = require('../dictionaries-entry/dist/esm/index.cjs');
const require_formatFillData = require('./formatFillData.cjs');
let node_path = require("node:path");

//#region ../@intlayer/cli/dist/esm/fill/writeFill.mjs
const writeFill = async (contentDeclarationFile, outputLocales, parentLocales, configuration) => {
	const appLogger = require_logger.getAppLogger(configuration);
	const fullDictionary = require_index.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 '${require_logger.colorizeKey(fullDictionary.key)}'`, { level: "info" });
		return;
	}
	const autoFillData = require_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 '${require_logger.colorizeKey(fullDictionary.key)}'`, { level: "error" });
			continue;
		}
		const { fill,...rest } = contentDeclarationFile;
		const relativeFilePath = (0, node_path.relative)(configuration.content.baseDir, output.filePath);
		await require_writeContentDeclaration.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 '${require_logger.colorizeKey(fullDictionary.key)}' written to ${require_formatter.formatPath(output.filePath)} for locale ${require_formatter.formatLocale(sourceLocale)}`, { level: "info" });
		} else appLogger(`Auto filled content declaration for '${require_logger.colorizeKey(fullDictionary.key)}' written to ${require_formatter.formatPath(output.filePath)}`, { level: "info" });
	}
};

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