'use client';


import { MessageKey } from "./editor/dist/esm/messagesKeys.mjs";
import { useCrossFrameMessageListener } from "./useCrossFrameMessageListener.mjs";
import { useCrossFrameState } from "./useCrossFrameState.mjs";
import { useDictionariesRecord } from "./DictionariesRecordContext.mjs";
import { useEditorLocale } from "./useEditorLocale.mjs";
import { editDictionaryByKeyPath } from "./core/dist/esm/dictionaryManipulator/editDictionaryByKeyPath.mjs";
import { getContentNodeByKeyPath } from "./core/dist/esm/dictionaryManipulator/getContentNodeByKeyPath.mjs";
import { renameContentNodeByKeyPath } from "./core/dist/esm/dictionaryManipulator/renameContentNodeByKeyPath.mjs";
import { NodeType } from "./types/dist/esm/nodeType.mjs";
import { createContext, useContext } from "react";
import { jsx } from "react/jsx-runtime";

//#region ../react-intlayer/dist/esm/@intlayer/editor-react/dist/esm/EditedContentContext.mjs
const EditedContentStateContext = createContext(void 0);
const useGetEditedContentState = (onEventTriggered) => useCrossFrameMessageListener(`${MessageKey.INTLAYER_EDITED_CONTENT_CHANGED}/get`, onEventTriggered);
const EditedContentActionsContext = createContext(void 0);
const resolveState = (state, prevState) => typeof state === "function" ? state(prevState) : state;
const EditedContentProvider = ({ children }) => {
	const { localeDictionaries } = useDictionariesRecord();
	const currentLocale = useEditorLocale();
	const [editedContent, setEditedContentState] = useCrossFrameState(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 getContentNodeByKeyPath(currentContent, newKeyPath) !== "undefined") {
					index++;
					finalKey = index === 0 ? lastKeyPath.key : `${lastKeyPath.key} (${index})`;
					newKeyPath = [...otherKeyPath, {
						...lastKeyPath,
						key: finalKey
					}];
				}
			}
			const updatedContent = 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 = 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 = editDictionaryByKeyPath(structuredClone(prev?.[localDictionaryId]?.content ?? originalContent), keyPath, 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 !== NodeType.Translation);
		if (localDictionaryIdOrKey.includes(":local:") || localDictionaryIdOrKey.includes(":remote:")) return getContentNodeByKeyPath(editedContent?.[localDictionaryIdOrKey]?.content ?? {}, filteredKeyPath, currentLocale);
		const filteredDictionariesLocalId = Object.keys(editedContent).filter((key) => key.startsWith(`${localDictionaryIdOrKey}:`));
		for (const localDictionaryId of filteredDictionariesLocalId) {
			const contentNode = getContentNodeByKeyPath(editedContent?.[localDictionaryId]?.content ?? {}, filteredKeyPath, currentLocale);
			if (contentNode) return contentNode;
		}
	};
	return /* @__PURE__ */ jsx(EditedContentStateContext.Provider, {
		value: { editedContent },
		children: /* @__PURE__ */ jsx(EditedContentActionsContext.Provider, {
			value: {
				setEditedContentState,
				setEditedDictionary,
				setEditedContent,
				addEditedContent,
				renameEditedContent,
				removeEditedContent,
				restoreEditedContent,
				clearEditedDictionaryContent,
				clearEditedContent,
				getEditedContentValue
			},
			children
		})
	});
};
const useEditedContentActions = () => useContext(EditedContentActionsContext);

//#endregion
export { EditedContentProvider, useEditedContentActions, useGetEditedContentState };
//# sourceMappingURL=EditedContentContext.mjs.map