/**
 * EditorRefPlugin exposes the Lexical editor instance to the parent TextEditor component:
 *   1. It receives a editorRef (a mutable ref object) from the parent
 *   2. Uses useLexicalComposerContext() to get the Lexical editor instance
 *   3. Stores the editor in the ref so the parent can access it
 *
 *   This is needed because Lexical's editor instance is only available inside the LexicalComposer
 *   context. The plugin bridges that gap, allowing TextEditor to use the editor in its imperative
 *   methods (ref.clear(), ref.setText(), ref.selectCurrentBlock(), etc.) which are defined outside
 *   the Lexical context via useImperativeHandle.
 */
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { type LexicalEditor } from 'lexical';
import type React from 'react';
import { useEffect } from 'react';

function EditorRefPlugin({ editorRef }: { editorRef: React.RefObject<LexicalEditor | null> }) {
  const [editor] = useLexicalComposerContext();

  useEffect(() => {
    editorRef.current = editor;
  }, [editor, editorRef]);

  return null;
}

export default EditorRefPlugin;
