"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { LexicalComposer } from "@lexical/react/LexicalComposer";
import { EditorConfigContext } from "./config/editor-config-context.js";
import { SharedHistoryContext } from "./context/shared-history-context.js";
import { SharedOnChangeContext } from "./context/shared-on-change-context.js";
import { Editor } from "./editor.js";
import { Nodes } from "./nodes/index.js";
import { ToolbarExtensionsProvider } from "./toolbar-extensions.js";
function EditorContext(props) {
    const { composerKey, editorConfig, onChange, readOnly, value, beforeEditor, afterEditor, children } = props;
    const initialConfig = useMemo(()=>({
            editable: true !== readOnly,
            editorState: null != value ? JSON.stringify(value) : void 0,
            namespace: editorConfig.lexical.namespace,
            nodes: [
                ...Nodes
            ],
            onError: (error)=>{
                throw error;
            },
            theme: editorConfig.lexical.theme
        }), [
        editorConfig,
        readOnly
    ]);
    if (null == initialConfig) return /*#__PURE__*/ jsx("p", {
        children: "Loading..."
    });
    return /*#__PURE__*/ jsx(LexicalComposer, {
        initialConfig: initialConfig,
        children: /*#__PURE__*/ jsx(EditorConfigContext, {
            config: editorConfig.settings,
            children: /*#__PURE__*/ jsx(SharedOnChangeContext, {
                onChange: onChange,
                children: /*#__PURE__*/ jsx(SharedHistoryContext, {
                    children: /*#__PURE__*/ jsx(ToolbarExtensionsProvider, {
                        children: /*#__PURE__*/ jsxs("div", {
                            className: "editor-shell",
                            children: [
                                beforeEditor,
                                /*#__PURE__*/ jsx(Editor, {
                                    minHeight: props.minHeight,
                                    maxHeight: props.maxHeight
                                }),
                                afterEditor,
                                children
                            ]
                        })
                    })
                })
            })
        })
    }, composerKey + initialConfig.editable);
}
export { EditorContext };
