import { NodeType } from "../types/dist/esm/nodeType.mjs";

//#region ../core/dist/esm/deepTransformPlugins/insertContentInDictionary.mjs
/**
* Deep merge helper that handles translation blocks
* @param target - The target object (dictionary content)
* @param source - The source object (new content to merge)
* @param locale - Optional locale to target specific translation
* @returns Merged content
*/
const deepMergeContent = (target, source, locale) => {
	if (source === null || source === void 0) return target;
	if (target === null || target === void 0) if (Array.isArray(source)) target = [];
	else if (typeof source === "object") target = {};
	else return source;
	if (target && typeof target === "object" && target.nodeType === NodeType.Translation) if (locale) {
		const existingLocaleContent = target.translation[locale];
		let newLocaleContent;
		if (typeof source === "object" && !Array.isArray(source)) newLocaleContent = deepMergeContent(existingLocaleContent, source, void 0);
		else if (Array.isArray(source)) newLocaleContent = source;
		else newLocaleContent = source;
		return {
			...target,
			translation: {
				...target.translation,
				[locale]: newLocaleContent
			}
		};
	} else if (typeof source === "object" && !Array.isArray(source) && source.nodeType === NodeType.Translation) return {
		...target,
		translation: {
			...target.translation,
			...source.translation
		}
	};
	else return processNewContent(source, locale);
	if (Array.isArray(source)) if (locale && Array.isArray(target)) {
		const result = [];
		const maxLength = Math.max(source.length, target.length);
		for (let i = 0; i < maxLength; i++) if (i < source.length) if (i < target.length && target[i] !== void 0) result[i] = deepMergeContent(target[i], source[i], locale);
		else result[i] = processNewContent(source[i], locale);
		else result[i] = target[i];
		return result;
	} else return source.map((item) => {
		if (item && typeof item === "object" && item.nodeType === NodeType.Translation) return item;
		return processNewContent(item, locale);
	});
	if (typeof source === "object" && !Array.isArray(source)) {
		if (typeof target !== "object" || Array.isArray(target)) target = {};
		const result = { ...target };
		for (const key in source) if (Object.hasOwn(source, key)) if (target[key] !== void 0) result[key] = deepMergeContent(target[key], source[key], locale);
		else result[key] = processNewContent(source[key], locale);
		return result;
	}
	return source;
};
/**
* Process new content that doesn't exist in target
* @param content - New content to process
* @param locale - Optional locale
* @returns Processed content
*/
const processNewContent = (content, locale) => {
	if (content === null || content === void 0) return content;
	if (content && typeof content === "object" && content.nodeType === NodeType.Translation) return content;
	if (Array.isArray(content)) return content.map((item) => processNewContent(item, locale));
	if (content && typeof content === "object") {
		const result = {};
		for (const key in content) if (Object.hasOwn(content, key)) result[key] = processNewContent(content[key], locale);
		return result;
	}
	if (locale) return {
		nodeType: NodeType.Translation,
		translation: { [locale]: content }
	};
	else return content;
};
/**
* Insert content into a dictionary with deep merge support
* Handles translation blocks and nested structures
*
* @param dictionary - The dictionary to insert content into
* @param content - The content to insert
* @param locale - Optional locale to target specific translation
* @returns Updated dictionary
*
* @example
* // Without locale - deep merge all content
* insertContentInDictionary(dictionary, { title: 'New Title' })
*
* @example
* // With locale - insert content for specific locale
* insertContentInDictionary(dictionary, { title: 'このページ' }, 'ja')
*/
const insertContentInDictionary = (dictionary, content, locale) => {
	const mergedContent = deepMergeContent(Array.isArray(dictionary.content) ? [...dictionary.content] : { ...dictionary.content }, content, locale);
	return {
		...dictionary,
		content: mergedContent
	};
};

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