'use client';

import { c as _c } from "react/compiler-runtime";
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext.js';
import { mergeRegister } from '@lexical/utils';
import { $getSelection, $isElementNode, $isRangeSelection, COMMAND_PRIORITY_LOW, PASTE_COMMAND } from 'lexical';
import { useEffect } from 'react';
import { validateUrl } from '../../../../../lexical/utils/url.js';
import { $toggleLink, LinkNode, TOGGLE_LINK_COMMAND } from '../../../nodes/LinkNode.js';
export const LinkPlugin = () => {
  const $ = _c(3);
  const [editor] = useLexicalComposerContext();
  let t0;
  let t1;
  if ($[0] !== editor) {
    t0 = () => {
      if (!editor.hasNodes([LinkNode])) {
        throw new Error("LinkPlugin: LinkNode not registered on editor");
      }
      return mergeRegister(editor.registerCommand(TOGGLE_LINK_COMMAND, _temp, COMMAND_PRIORITY_LOW), editor.registerCommand(PASTE_COMMAND, event => {
        const selection = $getSelection();
        if (!$isRangeSelection(selection) || selection.isCollapsed() || !(event instanceof ClipboardEvent) || event.clipboardData == null) {
          return false;
        }
        const clipboardText = event.clipboardData.getData("text");
        if (!validateUrl(clipboardText)) {
          return false;
        }
        if (!selection.getNodes().some(_temp2)) {
          const linkFields = {
            doc: null,
            linkType: "custom",
            newTab: false,
            url: clipboardText
          };
          editor.dispatchCommand(TOGGLE_LINK_COMMAND, {
            fields: linkFields,
            text: null
          });
          event.preventDefault();
          return true;
        }
        return false;
      }, COMMAND_PRIORITY_LOW));
    };
    t1 = [editor];
    $[0] = editor;
    $[1] = t0;
    $[2] = t1;
  } else {
    t0 = $[1];
    t1 = $[2];
  }
  useEffect(t0, t1);
  return null;
};
function _temp(payload) {
  $toggleLink(payload);
  return true;
}
function _temp2(node) {
  return $isElementNode(node);
}
//# sourceMappingURL=index.js.map