"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useMemo } from "react";
import { LexicalExtensionComposer } from "@lexical/react/LexicalExtensionComposer";
import { defineExtension } from "lexical";
import { defaultExtensionsList } from "./config/default-extensions.js";
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 { InlineImageExtension } from "./extensions/inline-image/inline-image-extension.js";
import { Nodes } from "./nodes/index.js";
function EditorContext(props) {
    const { composerKey, editorConfig, onChange, readOnly, value, beforeEditor, afterEditor, children } = props;
    const editable = true !== readOnly;
    const rootExtension = useMemo(()=>{
        const extensionsList = editorConfig.extensions ?? defaultExtensionsList();
        const dependencies = extensionsList.clone().configure(InlineImageExtension, {
            collection: editorConfig.settings.inlineImageUploadCollection
        }).toArray();
        return defineExtension({
            name: '[root]',
            namespace: editorConfig.lexical.namespace,
            nodes: [
                ...Nodes
            ],
            theme: editorConfig.lexical.theme,
            editable,
            $initialEditorState: null != value ? JSON.stringify(value) : void 0,
            onError: (error)=>{
                throw error;
            },
            dependencies
        });
    }, []);
    return /*#__PURE__*/ jsx(EditorConfigContext, {
        config: editorConfig.settings,
        children: /*#__PURE__*/ jsx(SharedOnChangeContext, {
            onChange: onChange,
            children: /*#__PURE__*/ jsx(SharedHistoryContext, {
                children: /*#__PURE__*/ jsx(LexicalExtensionComposer, {
                    extension: rootExtension,
                    contentEditable: null,
                    children: /*#__PURE__*/ jsxs("div", {
                        className: "editor-shell",
                        children: [
                            beforeEditor,
                            /*#__PURE__*/ jsx(Editor, {
                                minHeight: props.minHeight,
                                maxHeight: props.maxHeight
                            }),
                            afterEditor,
                            children
                        ]
                    })
                }, composerKey + editable)
            })
        })
    });
}
export { EditorContext };
