import { a as RichRenderer, i as mergeModules, n as wrapLazy, r as dedupNodes, t as composeRenderer } from "./compose-CCyeRK8c.js";
import { t as defineConfigurableModule } from "./configurable-module-Bur4JaI3.js";
import { Fragment, memo, useCallback, useMemo } from "react";
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
import { NestedContentRendererProvider, RichEditor } from "@haklex/rich-editor";
//#region src/core/compose-editor.tsx
function mergeSyncRenderers(modules) {
	const out = {};
	for (const m of modules) if (m.renderers) Object.assign(out, m.renderers);
	return out;
}
function mergeEditRenderers(modules) {
	const out = {};
	for (const m of modules) if (m.editRenderers) Object.assign(out, m.editRenderers);
	return out;
}
function composeProviders(modules, key) {
	const providers = modules.map((m) => m[key]).filter((P) => Boolean(P));
	if (providers.length === 0) return Fragment;
	const Composed = ({ children }) => {
		let acc = children;
		for (let i = providers.length - 1; i >= 0; i--) {
			const P = providers[i];
			acc = /* @__PURE__ */ jsx(P, { children: acc });
		}
		return /* @__PURE__ */ jsx(Fragment$1, { children: acc });
	};
	Composed.displayName = key === "Provider" ? "ComposedProviders" : "ComposedEditorProviders";
	return Composed;
}
function composeEditor(opts) {
	const merged = mergeModules(opts.preset, opts.modules);
	const baseNodes = merged.flatMap((m) => m.nodes ?? []);
	const editNodes = merged.flatMap((m) => m.editNodes ?? []);
	const allNodes = dedupNodes([...baseNodes, ...editNodes]);
	const syncR = mergeSyncRenderers(merged);
	const lazyR = wrapLazy(merged);
	const editR = mergeEditRenderers(merged);
	const finalConfig = {
		...syncR,
		...lazyR,
		...editR,
		...opts.overrides
	};
	const ComposedProviders = composeProviders(merged, "Provider");
	const ComposedEditorProviders = composeProviders(merged, "EditorProvider");
	const modulePlugins = [];
	const moduleActions = [];
	for (const m of merged) {
		if (m.plugins) modulePlugins.push(/* @__PURE__ */ jsx(Fragment, { children: m.plugins }, `p:${m.name}`));
		if (m.actions) moduleActions.push(/* @__PURE__ */ jsx(Fragment, { children: m.actions }, `a:${m.name}`));
	}
	const NestedRenderer = composeRenderer({
		preset: opts.preset,
		modules: opts.modules,
		overrides: opts.overrides
	});
	function ComposedEditor(props) {
		const { extraNodes: userExtraNodes, actions, children, theme, variant, ...rest } = props;
		const mergedExtraNodes = useMemo(() => userExtraNodes ? dedupNodes([...allNodes, ...userExtraNodes]) : allNodes, [userExtraNodes]);
		return /* @__PURE__ */ jsx(NestedContentRendererProvider, {
			value: useCallback((value, overrideVariant) => /* @__PURE__ */ jsx(NestedRenderer, {
				nested: true,
				theme,
				value,
				variant: overrideVariant ?? variant
			}), [theme, variant]),
			children: /* @__PURE__ */ jsx(ComposedProviders, { children: /* @__PURE__ */ jsx(ComposedEditorProviders, { children: /* @__PURE__ */ jsxs(RichEditor, {
				...rest,
				extraNodes: mergedExtraNodes,
				rendererConfig: finalConfig,
				theme,
				variant,
				actions: /* @__PURE__ */ jsxs(Fragment$1, { children: [moduleActions, actions] }),
				children: [modulePlugins, children]
			}) }) })
		});
	}
	ComposedEditor.displayName = "ComposedEditor";
	return memo(ComposedEditor);
}
//#endregion
export { RichRenderer, composeEditor, composeRenderer, dedupNodes, defineConfigurableModule, mergeModules, wrapLazy };
