'use client';

import { c as _c } from "react/compiler-runtime";
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { mergeRegister } from '@lexical/utils';
import { $createNodeSelection, $getNearestNodeFromDOMNode, $getSelection, $isDecoratorNode, $isNodeSelection, $setSelection, CLICK_COMMAND, COMMAND_PRIORITY_LOW, KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND } from 'lexical';
import { useEffect } from 'react';
// TODO: This should ideally be fixed in Lexical. See
// https://github.com/facebook/lexical/pull/7072
export function DecoratorPlugin() {
  const $ = _c(3);
  const [editor] = useLexicalComposerContext();
  const $onDelete = _temp2;
  let t0;
  let t1;
  if ($[0] !== editor) {
    t0 = () => mergeRegister(editor.registerCommand(CLICK_COMMAND, _temp3, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_DELETE_COMMAND, $onDelete, COMMAND_PRIORITY_LOW), editor.registerCommand(KEY_BACKSPACE_COMMAND, $onDelete, COMMAND_PRIORITY_LOW));
    t1 = [editor];
    $[0] = editor;
    $[1] = t0;
    $[2] = t1;
  } else {
    t0 = $[1];
    t1 = $[2];
  }
  useEffect(t0, t1);
  return null;
}
function _temp3(event_0) {
  document.querySelector(".decorator-selected")?.classList.remove("decorator-selected");
  const decorator = $getDecorator(event_0);
  if (!decorator) {
    return true;
  }
  const {
    decoratorElement,
    decoratorNode
  } = decorator;
  const {
    target
  } = event_0;
  const isInteractive = !(target instanceof HTMLElement) || target.isContentEditable || target.closest("button, textarea, input, .react-select, .code-editor, .no-select-decorator, [role=\"button\"]");
  if (isInteractive) {
    $setSelection(null);
  } else {
    const selection_0 = $createNodeSelection();
    selection_0.add(decoratorNode.getKey());
    $setSelection(selection_0);
    decoratorElement.classList.add("decorator-selected");
  }
  return true;
}
function _temp2(event) {
  const selection = $getSelection();
  if (!$isNodeSelection(selection)) {
    return false;
  }
  event.preventDefault();
  selection.getNodes().forEach(_temp);
  return true;
}
function _temp(node) {
  node.remove();
}
function $getDecorator(event) {
  if (!(event.target instanceof Element)) {
    return undefined;
  }
  const decoratorElement = event.target.closest('[data-lexical-decorator="true"]');
  if (!decoratorElement) {
    return undefined;
  }
  const node = $getNearestNodeFromDOMNode(decoratorElement);
  return $isDecoratorNode(node) ? {
    decoratorElement,
    decoratorNode: node
  } : undefined;
}
//# sourceMappingURL=index.js.map