const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_index = require('../unmerged-dictionaries-entry/dist/esm/index.cjs');
const require_getLocalizedContent = require('../core/dist/esm/deepTransformPlugins/getLocalizedContent.cjs');
const require_getFormatFromExtension = require('../utils/getFormatFromExtension.cjs');
const require_writeJSFile = require('./writeJSFile.cjs');
const require_getFilteredLocalesContent = require('../core/dist/esm/deepTransformPlugins/getFilteredLocalesContent.cjs');
const require_processContentDeclarationContent = require('./processContentDeclarationContent.cjs');
let node_path = require("node:path");
let node_fs_promises = require("node:fs/promises");
let deep_equal = require("deep-equal");
deep_equal = require_rolldown_runtime.__toESM(deep_equal);

//#region ../chokidar/dist/esm/writeContentDeclaration/writeContentDeclaration.mjs
const formatContentDeclaration = async (dictionary, configuration, localeList) => {
	/**
	* Clean Markdown, Insertion, File, etc. node metadata
	*/
	const processedDictionary = await require_processContentDeclarationContent.processContentDeclarationContent(dictionary);
	let content = processedDictionary.content;
	/**
	* Filter locales content
	*/
	if (dictionary.locale) content = require_getLocalizedContent.getPerLocaleDictionary(processedDictionary, dictionary.locale).content;
	else if (localeList) content = require_getFilteredLocalesContent.getFilteredLocalesDictionary(processedDictionary, localeList).content;
	let pluginFormatResult = {
		...dictionary,
		content
	};
	/**
	* Format the dictionary with the plugins
	*/
	for await (const plugin of configuration.plugins ?? []) if (plugin.formatOutput) {
		const formattedResult = await plugin.formatOutput?.({
			dictionary: pluginFormatResult,
			configuration
		});
		if (formattedResult) pluginFormatResult = formattedResult;
	}
	if (!(pluginFormatResult.content && pluginFormatResult.key)) return pluginFormatResult;
	let result = {
		key: dictionary.key,
		id: dictionary.id,
		title: dictionary.title,
		description: dictionary.description,
		tags: dictionary.tags,
		locale: dictionary.locale,
		fill: dictionary.fill,
		filled: dictionary.filled,
		priority: dictionary.priority,
		live: dictionary.live,
		version: dictionary.version,
		content
	};
	if (require_getFormatFromExtension.getFormatFromExtension(dictionary.filePath ? (0, node_path.extname)(dictionary.filePath) : ".json") === "json" && pluginFormatResult.content && pluginFormatResult.key) result = {
		$schema: "https://intlayer.org/schema.json",
		...result
	};
	return result;
};
const defaultOptions = { newDictionariesPath: "intlayer-dictionaries" };
const writeContentDeclaration = async (dictionary, configuration, options) => {
	const { content } = configuration;
	const { baseDir } = content;
	const { newDictionariesPath, localeList } = {
		...defaultOptions,
		...options
	};
	const newDictionaryLocationPath = (0, node_path.join)(baseDir, newDictionariesPath);
	const existingDictionary = require_index.getUnmergedDictionaries(configuration)[dictionary.key]?.find((el) => el.localId === dictionary.localId);
	const formattedContentDeclaration = await formatContentDeclaration(dictionary, configuration, localeList);
	if (existingDictionary?.filePath) {
		const isSameContent = (0, deep_equal.default)(existingDictionary, dictionary);
		const filePath = (0, node_path.resolve)(configuration.content.baseDir, existingDictionary.filePath);
		if (isSameContent) return {
			status: "up-to-date",
			path: filePath
		};
		await writeFileWithDirectories(filePath, formattedContentDeclaration, configuration);
		return {
			status: "updated",
			path: filePath
		};
	}
	if (dictionary.filePath) {
		const filePath = (0, node_path.resolve)(configuration.content.baseDir, dictionary.filePath);
		await writeFileWithDirectories(filePath, formattedContentDeclaration, configuration);
		return {
			status: "created",
			path: filePath
		};
	}
	const contentDeclarationPath = (0, node_path.join)(newDictionaryLocationPath, `${dictionary.key}.content.json`);
	await writeFileWithDirectories(contentDeclarationPath, formattedContentDeclaration, configuration);
	return {
		status: "imported",
		path: contentDeclarationPath
	};
};
const writeFileWithDirectories = async (absoluteFilePath, dictionary, configuration) => {
	await (0, node_fs_promises.mkdir)((0, node_path.dirname)(absoluteFilePath), { recursive: true });
	const extension = (0, node_path.extname)(absoluteFilePath);
	if (!configuration.content.fileExtensions.map((extension$1) => (0, node_path.extname)(extension$1)).includes(extension)) throw new Error(`Invalid file extension: ${extension}, file: ${absoluteFilePath}`);
	if (extension === ".json") {
		await (0, node_fs_promises.writeFile)(absoluteFilePath, `${JSON.stringify(dictionary, null, 2)}\n`);
		return;
	}
	await require_writeJSFile.writeJSFile(absoluteFilePath, dictionary, configuration);
	try {
		await (0, node_fs_promises.rm)((0, node_path.join)(configuration.content.cacheDir, "intlayer-prepared.lock"), { recursive: true });
	} catch (error) {
		if (error && typeof error === "object" && "code" in error && error.code !== "ENOENT") throw error;
	}
};

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