'use client';


import { built_default } from "../@intlayer/config/dist/esm/built.mjs";
import { useDictionariesRecordActions } from "../@intlayer/editor-react/dist/esm/DictionariesRecordContext.mjs";
import { useEditorEnabled } from "../@intlayer/editor-react/dist/esm/EditorEnabledContext.mjs";
import { EditorProvider } from "../@intlayer/editor-react/dist/esm/EditorProvider.mjs";
import { useCrossURLPathSetter } from "../@intlayer/editor-react/dist/esm/useCrossURLPathState.mjs";
import { useIframeClickInterceptor } from "../@intlayer/editor-react/dist/esm/useIframeClickInterceptor.mjs";
import { getUnmergedDictionaries } from "../@intlayer/unmerged-dictionaries-entry/dist/esm/index.mjs";
import { useEffect } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";

//#region ../react-intlayer/dist/esm/editor/IntlayerEditorProvider.mjs
const IntlayerEditorHooksEnabled = () => {
	/**
	* URL Messages
	*/
	useCrossURLPathSetter();
	/**
	* Click Messages
	*/
	useIframeClickInterceptor();
	/**
	* Sent local dictionaries to editor
	*/
	const { setLocaleDictionaries } = useDictionariesRecordActions();
	useEffect(() => {
		const unmergedDictionaries = getUnmergedDictionaries();
		setLocaleDictionaries(Object.fromEntries(Object.values(unmergedDictionaries).flat().map((dictionary) => [dictionary.localId, dictionary])));
	}, []);
	return /* @__PURE__ */ jsx(Fragment, {});
};
const { editor } = built_default ?? {};
const IntlayerEditorHook = () => {
	const { enabled } = useEditorEnabled();
	return enabled ? /* @__PURE__ */ jsx(IntlayerEditorHooksEnabled, {}) : /* @__PURE__ */ jsx(Fragment, {});
};
const IntlayerEditorProvider = ({ children }) => {
	return /* @__PURE__ */ jsxs(EditorProvider, {
		postMessage: (data) => {
			if (typeof window === "undefined") return;
			if (!editor) return;
			if (!(window.self !== window.top)) return;
			if (editor.applicationURL.length > 0) window?.postMessage(data, editor.applicationURL);
			if (editor.editorURL.length > 0) window.parent?.postMessage(data, editor.editorURL);
			if (editor.cmsURL.length > 0) window.parent?.postMessage(data, editor.cmsURL);
		},
		allowedOrigins: [
			editor?.editorURL,
			editor?.cmsURL,
			editor?.applicationURL
		].filter(Boolean),
		mode: "client",
		configuration: built_default,
		children: [/* @__PURE__ */ jsx(IntlayerEditorHook, {}), children]
	});
};

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