'use client';


const require_rolldown_runtime = require('../../../../../../../_virtual/rolldown_runtime.cjs');
const require_messagesKeys = require('./editor/dist/esm/messagesKeys.cjs');
const require_useCrossFrameMessageListener = require('./useCrossFrameMessageListener.cjs');
const require_useCrossFrameState = require('./useCrossFrameState.cjs');
const require_DictionariesRecordContext = require('./DictionariesRecordContext.cjs');
const require_useEditorLocale = require('./useEditorLocale.cjs');
const require_editDictionaryByKeyPath = require('./core/dist/esm/dictionaryManipulator/editDictionaryByKeyPath.cjs');
const require_getContentNodeByKeyPath = require('./core/dist/esm/dictionaryManipulator/getContentNodeByKeyPath.cjs');
const require_renameContentNodeByKeyPath = require('./core/dist/esm/dictionaryManipulator/renameContentNodeByKeyPath.cjs');
const require_nodeType = require('./types/dist/esm/nodeType.cjs');
let react = require("react");
let react_jsx_runtime = require("react/jsx-runtime");

//#region ../react-intlayer/dist/esm/@intlayer/editor-react/dist/esm/EditedContentContext.mjs
const EditedContentStateContext = (0, react.createContext)(void 0);
const useGetEditedContentState = (onEventTriggered) => require_useCrossFrameMessageListener.useCrossFrameMessageListener(`${require_messagesKeys.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
const EditedContentActionsContext = (0, react.createContext)(void 0);
const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
const EditedContentProvider = ({ children }) => {
	const { localeDictionaries } = require_DictionariesRecordContext.useDictionariesRecord();
	const currentLocale = require_useEditorLocale.useEditorLocale();
	const [editedContent, setEditedContentState] = require_useCrossFrameState.useCrossFrameState(require_messagesKeys.MessageKey.INTLAYER_EDITED_CONTENT_CHANGED);
	const setEditedDictionary = (newValue) => {
		let updatedDictionaries = resolveState(newValue);
		setEditedContentState((prev) => {
			if (!updatedDictionaries.localId) {
				console.error("no localId", updatedDictionaries);
				return prev;
			}
			updatedDictionaries = resolveState(newValue, prev?.[updatedDictionaries.localId]);
			return {
				...prev,
				[updatedDictionaries.localId]: updatedDictionaries
			};
		});
		return updatedDictionaries;
	};
	const setEditedContent = (localDictionaryId, newValue) => {
		setEditedContentState((prev) => ({
			...prev,
			[localDictionaryId]: {
				...prev?.[localDictionaryId],
				content: newValue
			}
		}));
	};
	const addEditedContent = (localDictionaryId, newValue, keyPath = [], overwrite = true) => {
		setEditedContentState((prev) => {
			const originalContent = localeDictionaries[localDictionaryId]?.content;
			const currentContent = structuredClone(prev?.[localDictionaryId]?.content ?? originalContent);
			let newKeyPath = keyPath;
			if (!overwrite) {
				let index = 0;
				const otherKeyPath = keyPath.slice(0, -1);
				const lastKeyPath = keyPath[keyPath.length - 1];
				let finalKey = lastKeyPath.key;
				while (typeof require_getContentNodeByKeyPath.getContentNodeByKeyPath(currentContent, newKeyPath) !== "undefined") {
					index++;
					finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
					newKeyPath = [...otherKeyPath, {
						...lastKeyPath,
						key: finalKey
					}];
				}
			}
			const updatedContent = require_editDictionaryByKeyPath.editDictionaryByKeyPath(currentContent, newKeyPath, newValue);
			return {
				...prev,
				[localDictionaryId]: {
					...prev?.[localDictionaryId],
					content: updatedContent
				}
			};
		});
	};
	const renameEditedContent = (localDictionaryId, newKey, keyPath = []) => {
		setEditedContentState((prev) => {
			const originalContent = localeDictionaries[localDictionaryId]?.content;
			const contentWithNewField = require_renameContentNodeByKeyPath.renameContentNodeByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), newKey, keyPath);
			return {
				...prev,
				[localDictionaryId]: {
					...prev?.[localDictionaryId],
					content: contentWithNewField
				}
			};
		});
	};
	const removeEditedContent = (localDictionaryId, keyPath) => {
		setEditedContentState((prev) => {
			const originalContent = localeDictionaries[localDictionaryId]?.content;
			const restoredContent = require_editDictionaryByKeyPath.editDictionaryByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, require_getContentNodeByKeyPath.getContentNodeByKeyPath(originalContent, keyPath));
			return {
				...prev,
				[localDictionaryId]: {
					...prev?.[localDictionaryId],
					content: restoredContent
				}
			};
		});
	};
	const restoreEditedContent = (localDictionaryId) => {
		setEditedContentState((prev) => {
			const updated = { ...prev };
			delete updated[localDictionaryId];
			return updated;
		});
	};
	const clearEditedDictionaryContent = (localDictionaryId) => {
		setEditedContentState((prev) => {
			return Object.entries(prev).reduce((acc, [key, value]) => {
				if (key === localDictionaryId) return acc;
				return {
					...acc,
					[key]: value
				};
			}, {});
		});
	};
	const clearEditedContent = () => {
		setEditedContentState({});
	};
	const getEditedContentValue = (localDictionaryIdOrKey, keyPath) => {
		if (!editedContent) return void 0;
		const filteredKeyPath = keyPath.filter((key) => key.type !== require_nodeType.NodeType.Translation);
		if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return require_getContentNodeByKeyPath.getContentNodeByKeyPath(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, filteredKeyPath, currentLocale);
		const filteredDictionariesLocalId = Object.keys(editedContent).filter((key) => key.startsWith(`${localDictionaryIdOrKey}:`));
		for (const localDictionaryId of filteredDictionariesLocalId) {
			const contentNode = require_getContentNodeByKeyPath.getContentNodeByKeyPath(editedContent?.[localDictionaryId]?.content ?? {}, filteredKeyPath, currentLocale);
			if (contentNode) return contentNode;
		}
	};
	return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditedContentStateContext.Provider, {
		value: { editedContent },
		children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(EditedContentActionsContext.Provider, {
			value: {
				setEditedContentState,
				setEditedDictionary,
				setEditedContent,
				addEditedContent,
				renameEditedContent,
				removeEditedContent,
				restoreEditedContent,
				clearEditedDictionaryContent,
				clearEditedContent,
				getEditedContentValue
			},
			children
		})
	});
};
const useEditedContentActions = () => (0, react.useContext)(EditedContentActionsContext);

//#endregion
exports.EditedContentProvider = EditedContentProvider;
exports.useEditedContentActions = useEditedContentActions;
exports.useGetEditedContentState = useGetEditedContentState;
//# sourceMappingURL=EditedContentContext.cjs.map