'use client';

import { c as _c } from "react/compiler-runtime";
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js';
import { useLexicalNodeSelection } from '@lexical/react/useLexicalNodeSelection.js';
import { addClassNamesToElement, mergeRegister, removeClassNamesFromElement } from '@lexical/utils';
import { $getSelection, $isNodeSelection, CLICK_COMMAND, COMMAND_PRIORITY_LOW, KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND } from 'lexical';
import { useCallback, useEffect } from 'react';
import { $isHorizontalRuleNode } from '../nodes/HorizontalRuleNode.js';
const isSelectedClassName = 'selected';
/**
 * React component rendered in the lexical editor, WITHIN the hr element created by createDOM of the HorizontalRuleNode.
 *
 * @param nodeKey every node has a unique key (this key is not saved to the database and thus may differ between sessions). It's useful for working with the CURRENT lexical editor state
 */
export function HorizontalRuleComponent(t0) {
  const $ = _c(16);
  const {
    nodeKey
  } = t0;
  const [editor] = useLexicalComposerContext();
  const [isSelected, setSelected, clearSelection] = useLexicalNodeSelection(nodeKey);
  let t1;
  if ($[0] !== editor || $[1] !== isSelected) {
    t1 = event => {
      const deleteSelection = $getSelection();
      if (isSelected && $isNodeSelection(deleteSelection)) {
        event.preventDefault();
        editor.update(() => {
          deleteSelection.getNodes().forEach(_temp);
        });
      }
      return false;
    };
    $[0] = editor;
    $[1] = isSelected;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const $onDelete = t1;
  let t2;
  let t3;
  if ($[3] !== $onDelete || $[4] !== clearSelection || $[5] !== editor || $[6] !== isSelected || $[7] !== nodeKey || $[8] !== setSelected) {
    t2 = () => mergeRegister(editor.registerCommand(CLICK_COMMAND, event_0 => {
      const hrElem = editor.getElementByKey(nodeKey);
      if (event_0.target === hrElem) {
        if (!event_0.shiftKey) {
          clearSelection();
        }
        setSelected(!isSelected);
        return true;
      }
      return false;
    }, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_DELETE_COMMAND, $onDelete, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_BACKSPACE_COMMAND, $onDelete, COMMAND_PRIORITY_LOW));
    t3 = [clearSelection, editor, isSelected, nodeKey, $onDelete, setSelected];
    $[3] = $onDelete;
    $[4] = clearSelection;
    $[5] = editor;
    $[6] = isSelected;
    $[7] = nodeKey;
    $[8] = setSelected;
    $[9] = t2;
    $[10] = t3;
  } else {
    t2 = $[9];
    t3 = $[10];
  }
  useEffect(t2, t3);
  let t4;
  let t5;
  if ($[11] !== editor || $[12] !== isSelected || $[13] !== nodeKey) {
    t4 = () => {
      const hrElem_0 = editor.getElementByKey(nodeKey);
      if (hrElem_0 !== null) {
        if (isSelected) {
          addClassNamesToElement(hrElem_0, isSelectedClassName);
        } else {
          removeClassNamesFromElement(hrElem_0, isSelectedClassName);
        }
      }
    };
    t5 = [editor, isSelected, nodeKey];
    $[11] = editor;
    $[12] = isSelected;
    $[13] = nodeKey;
    $[14] = t4;
    $[15] = t5;
  } else {
    t4 = $[14];
    t5 = $[15];
  }
  useEffect(t4, t5);
  return null;
}
function _temp(node) {
  if ($isHorizontalRuleNode(node)) {
    node.remove();
  }
}
//# sourceMappingURL=index.js.map