import { deepTransformNode } from "../core/dist/esm/interpreter/getContent/deepTransform.mjs";
import { NodeType } from "../types/dist/esm/nodeType.mjs";
import { dirname, join } from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { colorizePath, x } from "@intlayer/config";

//#region ../chokidar/dist/esm/writeContentDeclaration/processContentDeclarationContent.mjs
/**
* Write file plugin
*/
const writeFilePlugin = {
	id: "write-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.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 = join(process.cwd(), filePath);
			const fileDirectory = dirname(absoluteFilePath);
			if (!existsSync(fileDirectory)) mkdirSync(fileDirectory, { recursive: true });
			writeFileSync(absoluteFilePath, fileContent);
		} catch (error) {
			throw new Error(`${x} Error writing file to ${colorizePath(filePath)}: ${error}`);
		}
		return {
			nodeType: NodeType.File,
			[NodeType.File]: node.file
		};
	}
};
/**
* Markdown file plugin
*/
const markdownFilePlugin = {
	id: "markdown-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Markdown,
	transform: (node, props, deepTransformNode$1) => {
		return {
			nodeType: NodeType.Markdown,
			[NodeType.Markdown]: deepTransformNode$1(node.markdown, props)
		};
	}
};
/**
* Insertion file plugin
*/
const insertionFilePlugin = {
	id: "insertion-file-plugin",
	canHandle: (node) => typeof node === "object" && node?.nodeType === NodeType.Insertion,
	transform: (node, props, deepTransformNode$1) => {
		return {
			nodeType: NodeType.Insertion,
			[NodeType.Insertion]: deepTransformNode$1(node.insertion, props)
		};
	}
};
const processContentDeclarationContent = async (dictionary) => deepTransformNode(dictionary, {
	dictionaryKey: dictionary.key,
	keyPath: [],
	plugins: [
		writeFilePlugin,
		markdownFilePlugin,
		insertionFilePlugin
	]
});

//#endregion
export { processContentDeclarationContent };
//# sourceMappingURL=processContentDeclarationContent.mjs.map