import { NodeType } from "../types/dist/esm/nodeType.mjs";
import { deepTransformNode } from "../interpreter/getContent/deepTransform.mjs";
import { built_default } from "../config/dist/esm/built.mjs";
import { getTranslation } from "../interpreter/getTranslation.mjs";

//#region ../@intlayer/cli/dist/esm/core/dist/esm/deepTransformPlugins/getFilterTranslationsOnlyContent.mjs
/**
* Helper function to check if a node or its children contain translation nodes
*/
const hasTranslationNodes = (node) => {
	if (typeof node !== "object" || node === null) return false;
	if (node?.nodeType === NodeType.Translation) return true;
	if (Array.isArray(node)) return node.some(hasTranslationNodes);
	return Object.values(node).some(hasTranslationNodes);
};
/** Translation plugin. Replaces node with a locale string if nodeType = Translation. */
const filterTranslationsOnlyPlugin = (locale, fallback) => ({
	id: "filter-translations-only-plugin",
	canHandle: (node) => {
		return typeof node === "object" && node !== null;
	},
	transform: (node, props, deepTransformNode$1) => {
		if (typeof node === "object" && node?.nodeType === NodeType.Translation) {
			const result = structuredClone(node[NodeType.Translation]);
			for (const key in result) {
				const childProps = {
					...props,
					children: result[key],
					keyPath: [...props.keyPath, {
						type: NodeType.Translation,
						key
					}]
				};
				result[key] = deepTransformNode$1(result[key], {
					...childProps,
					plugins: [...(props.plugins ?? []).filter((plugin) => plugin.id !== "filter-translations-only-plugin")]
				});
			}
			return getTranslation(result, locale, fallback);
		} else if (typeof node === "object" && node !== null && !Array.isArray(node) && !node?.nodeType) {
			const result = {};
			for (const key in node) if (hasTranslationNodes(node[key])) {
				const childProps = {
					...props,
					children: node[key],
					keyPath: [...props.keyPath, {
						type: NodeType.Object,
						key
					}]
				};
				result[key] = deepTransformNode$1(node[key], childProps);
			}
			return result;
		} else if (Array.isArray(node)) return node.map((child, index) => {
			return deepTransformNode$1(child, {
				...props,
				children: child,
				keyPath: [...props.keyPath, {
					type: NodeType.Array,
					key: index
				}]
			});
		});
		return "to remove from the object";
	}
});
/**
* Return the content of a node with only the translation plugin.
*
* @param node The node to transform.
* @param locale The locale to use if your transformers need it (e.g. for translations).
*/
const getFilterTranslationsOnlyContent = (node, locale = built_default?.internationalization?.defaultLocale, nodeProps, fallback) => {
	const plugins = [filterTranslationsOnlyPlugin(locale, fallback), ...nodeProps.plugins ?? []];
	return deepTransformNode(node, {
		...nodeProps,
		plugins
	});
};
const getFilterTranslationsOnlyDictionary = (dictionary, locale = built_default?.internationalization?.defaultLocale, fallback) => ({
	...dictionary,
	content: getFilterTranslationsOnlyContent(dictionary.content, locale, {
		dictionaryKey: dictionary.key,
		keyPath: []
	}, fallback)
});

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