"use client";
import { createLexicalComposerContext as v, LexicalComposerContext as w } from "./LexicalComposerContext.prod.js";
import { createEditor as x, $getRoot as m, $createParagraphNode as C, $getSelection as S, HISTORY_MERGE_TAG as y } from "../../lexical/Lexical.prod.js";
import { useMemo as L, useLayoutEffect as R, useEffect as $ } from "react";
import { jsx as P } from "react/jsx-runtime";
const u = typeof window < "u" && window.document !== void 0 && window.document.createElement !== void 0, T = u ? R : $, i = { tag: y };
function N({ initialConfig: r, children: p }) {
  const c = L((() => {
    const { theme: o, namespace: s, nodes: f, onError: E, editorState: h, html: b } = r, g = v(null, o), a = x({ editable: r.editable, html: b, namespace: s, nodes: f, onError: (e) => E(e, a), theme: o });
    return (function(e, t) {
      if (t !== null) {
        if (t === void 0) e.update((() => {
          const n = m();
          if (n.isEmpty()) {
            const l = C();
            n.append(l);
            const d = u ? document.activeElement : null;
            (S() !== null || d !== null && d === e.getRootElement()) && l.select();
          }
        }), i);
        else if (t !== null) switch (typeof t) {
          case "string": {
            const n = e.parseEditorState(t);
            e.setEditorState(n, i);
            break;
          }
          case "object":
            e.setEditorState(t, i);
            break;
          case "function":
            e.update((() => {
              m().isEmpty() && t(e);
            }), i);
        }
      }
    })(a, h), [a, g];
  }), []);
  return T((() => {
    const o = r.editable, [s] = c;
    s.setEditable(o === void 0 || o);
  }), []), P(w.Provider, { value: c, children: p });
}
export {
  N as LexicalComposer
};
