'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const index = require('../../../../../../../node_modules/.pnpm/@tiptap_react@3.15.3_@floating-ui_dom@1.7.4_@tiptap_core@3.20.0_@tiptap_pm@3.15.3__@tip_b42b5b2d59902d8a01cd4cc3e2d5dc64/node_modules/@tiptap/react/dist/menus/index.cjs');
const linkEditBlock = require('../link/link-edit-block.cjs');
const linkPopoverBlock = require('../link/link-popover-block.cjs');

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  const n = Object.create(null);
  if (e) {
    for (const k in e) {
      n[k] = e[k];
    }
  }
  n.default = e;
  return n;
}

const React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

const LinkBubbleMenu = ({ editor }) => {
  const [showEdit, setShowEdit] = React__namespace.useState(false);
  const [linkAttrs, setLinkAttrs] = React__namespace.useState({
    href: "",
    target: ""
  });
  const [selectedText, setSelectedText] = React__namespace.useState("");
  const updateLinkState = React__namespace.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__namespace.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__namespace.useCallback(() => {
    setShowEdit(true);
  }, []);
  const onSetLink = React__namespace.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__namespace.useCallback(() => {
    editor.chain().focus().extendMarkRange("link").unsetLink().run();
    setShowEdit(false);
    updateLinkState();
  }, [editor, updateLinkState]);
  return /* @__PURE__ */ jsxRuntime.jsx(
    index.BubbleMenu,
    {
      editor,
      shouldShow,
      options: {
        placement: "bottom-start",
        onHide: () => setShowEdit(false)
      },
      children: showEdit ? /* @__PURE__ */ jsxRuntime.jsx(
        linkEditBlock.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__ */ jsxRuntime.jsx(
        linkPopoverBlock.LinkPopoverBlock,
        {
          onClear: onUnsetLink,
          url: linkAttrs.href,
          onEdit: handleEdit
        }
      )
    }
  );
};

exports.LinkBubbleMenu = LinkBubbleMenu;
