const require_rolldown_runtime = require('../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../../../../config/dist/esm/logger.cjs');
const require_getFormatFromExtension = require('../utils/getFormatFromExtension.cjs');
const require_getContentDeclarationFileTemplate = require('../getContentDeclarationFileTemplate/getContentDeclarationFileTemplate.cjs');
const require_transformJSFile = require('./transformJSFile.cjs');
const require_detectFormatCommand = require('./detectFormatCommand.cjs');
let node_child_process = require("node:child_process");
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");
let node_fs = require("node:fs");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/writeContentDeclaration/writeJSFile.mjs
/**
* Updates a JavaScript/TypeScript file based on the provided JSON instructions.
* It targets a specific dictionary object within the file (identified by its 'key' property)
* and updates its 'content' entries. Currently, it focuses on modifying arguments
* of 't' (translation) function calls.
*/
const writeJSFile = async (filePath, dictionary, configuration) => {
	const mergedDictionary = {
		...configuration.dictionary,
		...dictionary
	};
	const appLogger = require_logger.getAppLogger(configuration);
	if (!(0, node_fs.existsSync)(filePath)) {
		const format = require_getFormatFromExtension.getFormatFromExtension((0, node_path.extname)(filePath));
		appLogger("File does not exist, creating it", { isVerbose: true });
		await (0, node_fs_promises.writeFile)(filePath, await require_getContentDeclarationFileTemplate.getContentDeclarationFileTemplate(mergedDictionary.key, format, Object.fromEntries(Object.entries({
			id: mergedDictionary.id,
			locale: mergedDictionary.locale,
			filled: mergedDictionary.filled,
			fill: mergedDictionary.fill,
			description: mergedDictionary.description,
			title: mergedDictionary.title,
			tags: mergedDictionary.tags,
			version: mergedDictionary.version,
			priority: mergedDictionary.priority,
			live: mergedDictionary.live
		}).filter(([, value]) => value !== void 0))), "utf-8");
	}
	const finalCode = await require_transformJSFile.transformJSFile(await (0, node_fs_promises.readFile)(filePath, "utf-8"), dictionary);
	try {
		await (0, node_fs_promises.writeFile)(filePath, finalCode, "utf-8");
		require_logger.logger(`Successfully updated ${filePath}`, {
			level: "info",
			isVerbose: true
		});
	} catch (error) {
		const err = error;
		require_logger.logger(`Failed to write updated file: ${filePath}`, { level: "error" });
		throw new Error(`Failed to write updated file ${filePath}: ${err.message}`);
	}
	const formatCommand = require_detectFormatCommand.detectFormatCommand(configuration);
	if (formatCommand) try {
		(0, node_child_process.execSync)(formatCommand.replace("{{file}}", filePath), {
			stdio: "inherit",
			cwd: configuration.content.baseDir
		});
	} catch (error) {
		console.error(error);
	}
};

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