const require_rolldown_runtime = require('../../../../../../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../../../../../config/dist/esm/logger.cjs');
const require_deepTransform = require('../core/dist/esm/interpreter/getContent/deepTransform.cjs');
const require_nodeType = require('../types/dist/esm/nodeType.cjs');
let node_path = require("node:path");
let node_fs = require("node:fs");

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs
/**
* Write file plugin
*/
const writeFilePlugin = {
	id: "write-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.nodeType === require_nodeType.NodeType.File,
	transform: (node) => {
		const fileContent = node.content;
		const filePath = node.fixedPath;
		if (typeof fileContent !== "string") throw new Error("File content must be a string");
		if (typeof filePath !== "string") throw new Error("File path must be a string");
		try {
			const absoluteFilePath = (0, node_path.join)(process.cwd(), filePath);
			const fileDirectory = (0, node_path.dirname)(absoluteFilePath);
			if (!(0, node_fs.existsSync)(fileDirectory)) (0, node_fs.mkdirSync)(fileDirectory, { recursive: true });
			(0, node_fs.writeFileSync)(absoluteFilePath, fileContent);
		} catch (error) {
			throw new Error(`${require_logger.x} Error writing file to ${require_logger.colorizePath(filePath)}: ${error}`);
		}
		return {
			nodeType: require_nodeType.NodeType.File,
			[require_nodeType.NodeType.File]: node.file
		};
	}
};
/**
* Markdown file plugin
*/
const markdownFilePlugin = {
	id: "markdown-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.nodeType === require_nodeType.NodeType.Markdown,
	transform: (node, props, deepTransformNode$1) => {
		return {
			nodeType: require_nodeType.NodeType.Markdown,
			[require_nodeType.NodeType.Markdown]: deepTransformNode$1(node.markdown, props)
		};
	}
};
/**
* Insertion file plugin
*/
const insertionFilePlugin = {
	id: "insertion-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.nodeType === require_nodeType.NodeType.Insertion,
	transform: (node, props, deepTransformNode$1) => {
		return {
			nodeType: require_nodeType.NodeType.Insertion,
			[require_nodeType.NodeType.Insertion]: deepTransformNode$1(node.insertion, props)
		};
	}
};
const processContentDeclarationContent = async (dictionary) => require_deepTransform.deepTransformNode(dictionary, {
	dictionaryKey: dictionary.key,
	keyPath: [],
	plugins: [
		writeFilePlugin,
		markdownFilePlugin,
		insertionFilePlugin
	]
});

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