'use client';

import { c as _c } from "react/compiler-runtime";
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js';
import { $insertNodeToNearestRoot, $wrapNodeInElement, mergeRegister } from '@lexical/utils';
import { formatDrawerSlug, useEditDepth } from '@payloadcms/ui';
import { $createParagraphNode, $getNodeByKey, $getPreviousSelection, $getSelection, $insertNodes, $isParagraphNode, $isRangeSelection, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR } from 'lexical';
import { useEffect, useState } from 'react';
import { useEditorConfigContext } from '../../../../lexical/config/client/EditorConfigProvider.js';
import { useLexicalDrawer } from '../../../../utilities/fieldsDrawer/useLexicalDrawer.js';
import { $createBlockNode, BlockNode } from '../nodes/BlocksNode.js';
import { $createInlineBlockNode, $isInlineBlockNode } from '../nodes/InlineBlocksNode.js';
import { INSERT_BLOCK_COMMAND, INSERT_INLINE_BLOCK_COMMAND } from './commands.js';
export const BlocksPlugin = () => {
  const $ = _c(10);
  const [editor] = useLexicalComposerContext();
  const [targetNodeKey, setTargetNodeKey] = useState(null);
  const {
    uuid
  } = useEditorConfigContext();
  const editDepth = useEditDepth();
  const t0 = `lexical-inlineBlocks-create-` + uuid;
  let t1;
  if ($[0] !== editDepth || $[1] !== t0) {
    t1 = formatDrawerSlug({
      slug: t0,
      depth: editDepth
    });
    $[0] = editDepth;
    $[1] = t0;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const drawerSlug = t1;
  const {
    toggleDrawer
  } = useLexicalDrawer(drawerSlug, true);
  let t2;
  if ($[3] !== editor || $[4] !== targetNodeKey) {
    t2 = () => {
      if (!editor.hasNodes([BlockNode])) {
        throw new Error("BlocksPlugin: BlocksNode not registered on editor");
      }
      return mergeRegister(editor.registerCommand(INSERT_BLOCK_COMMAND, payload => {
        editor.update(() => {
          const selection = $getSelection() || $getPreviousSelection();
          if ($isRangeSelection(selection)) {
            const blockNode = $createBlockNode(payload);
            $insertNodeToNearestRoot(blockNode);
            const {
              focus
            } = selection;
            const focusNode = focus.getNode();
            if ($isParagraphNode(focusNode) && focusNode.getTextContentSize() === 0 && focusNode.getParentOrThrow().getChildren().filter(_temp).length > 1) {
              focusNode.remove();
            }
          }
        });
        return true;
      }, COMMAND_PRIORITY_EDITOR), editor.registerCommand(INSERT_INLINE_BLOCK_COMMAND, fields => {
        if (targetNodeKey) {
          const node_0 = $getNodeByKey(targetNodeKey);
          if (!node_0 || !$isInlineBlockNode(node_0)) {
            return false;
          }
          node_0.setFields(fields);
          setTargetNodeKey(null);
          return true;
        }
        const inlineBlockNode = $createInlineBlockNode(fields);
        $insertNodes([inlineBlockNode]);
        if ($isRootOrShadowRoot(inlineBlockNode.getParentOrThrow())) {
          $wrapNodeInElement(inlineBlockNode, $createParagraphNode).selectEnd();
        }
        return true;
      }, COMMAND_PRIORITY_EDITOR));
    };
    $[3] = editor;
    $[4] = targetNodeKey;
    $[5] = t2;
  } else {
    t2 = $[5];
  }
  let t3;
  if ($[6] !== editor || $[7] !== targetNodeKey || $[8] !== toggleDrawer) {
    t3 = [editor, targetNodeKey, toggleDrawer];
    $[6] = editor;
    $[7] = targetNodeKey;
    $[8] = toggleDrawer;
    $[9] = t3;
  } else {
    t3 = $[9];
  }
  useEffect(t2, t3);
  return null;
};
function _temp(node) {
  return $isParagraphNode(node);
}
//# sourceMappingURL=index.js.map