import { jsx } from 'react/jsx-runtime';
import * as React from 'react';
import { BubbleMenu } from '../../../../../../../node_modules/.pnpm/@tiptap_react@3.15.3_@floating-ui_dom@1.7.4_@tiptap_core@3.15.3_@tiptap_pm@3.15.3__@tip_d7a2a9a319317617a8908309649a6dda/node_modules/@tiptap/react/dist/menus/index.mjs';
import { LinkEditBlock } from '../link/link-edit-block.mjs';
import { LinkPopoverBlock } from '../link/link-popover-block.mjs';

const LinkBubbleMenu = ({ editor }) => {
  const [showEdit, setShowEdit] = React.useState(false);
  const [linkAttrs, setLinkAttrs] = React.useState({
    href: "",
    target: ""
  });
  const [selectedText, setSelectedText] = React.useState("");
  const updateLinkState = React.useCallback(() => {
    const { from, to } = editor.state.selection;
    const { href, target } = editor.getAttributes("link");
    const text = editor.state.doc.textBetween(from, to, " ");
    setLinkAttrs({ href, target });
    setSelectedText(text);
  }, [editor]);
  const shouldShow = React.useCallback(
    ({ editor: editor2, from, to }) => {
      if (from === to) {
        return false;
      }
      const { href } = editor2.getAttributes("link");
      if (!editor2.isActive("link") || !editor2.isEditable) {
        return false;
      }
      if (href) {
        updateLinkState();
        return true;
      }
      return false;
    },
    [updateLinkState]
  );
  const handleEdit = React.useCallback(() => {
    setShowEdit(true);
  }, []);
  const onSetLink = React.useCallback(
    (url, text, openInNewTab) => {
      editor.chain().focus().extendMarkRange("link").insertContent({
        type: "text",
        text: text || url,
        marks: [
          {
            type: "link",
            attrs: {
              href: url,
              target: openInNewTab ? "_blank" : ""
            }
          }
        ]
      }).setLink({ href: url, target: openInNewTab ? "_blank" : "" }).run();
      setShowEdit(false);
      updateLinkState();
    },
    [editor, updateLinkState]
  );
  const onUnsetLink = React.useCallback(() => {
    editor.chain().focus().extendMarkRange("link").unsetLink().run();
    setShowEdit(false);
    updateLinkState();
  }, [editor, updateLinkState]);
  return /* @__PURE__ */ jsx(
    BubbleMenu,
    {
      editor,
      shouldShow,
      options: {
        placement: "bottom-start",
        onHide: () => setShowEdit(false)
      },
      children: showEdit ? /* @__PURE__ */ jsx(
        LinkEditBlock,
        {
          defaultUrl: linkAttrs.href,
          defaultText: selectedText,
          defaultIsNewTab: linkAttrs.target === "_blank",
          onSave: onSetLink,
          className: "bg-popover text-popover-foreground w-full min-w-80 rounded-md border p-4 shadow-md outline-hidden"
        }
      ) : /* @__PURE__ */ jsx(
        LinkPopoverBlock,
        {
          onClear: onUnsetLink,
          url: linkAttrs.href,
          onEdit: handleEdit
        }
      )
    }
  );
};

export { LinkBubbleMenu };
