import { jsxs, jsx } from 'react/jsx-runtime';
import * as React from 'react';
import { Popover, PopoverTrigger, PopoverContent } from '../../../popover.mjs';
import { Link2Icon } from '../../../../../../../node_modules/.pnpm/@radix-ui_react-icons@1.3.2_react@19.2.0/node_modules/@radix-ui/react-icons/dist/react-icons.esm.mjs';
import { ToolbarButton } from '../toolbar-button.mjs';
import { LinkEditBlock } from './link-edit-block.mjs';

const LinkEditPopover = ({ editor, size, variant }) => {
  const [open, setOpen] = React.useState(false);
  const { from, to } = editor.state.selection;
  const text = editor.state.doc.textBetween(from, to, " ");
  const onSetLink = React.useCallback(
    (url, text2, openInNewTab) => {
      editor.chain().focus().extendMarkRange("link").insertContent({
        type: "text",
        text: text2 || url,
        marks: [
          {
            type: "link",
            attrs: {
              href: url,
              target: openInNewTab ? "_blank" : ""
            }
          }
        ]
      }).setLink({ href: url }).run();
      editor.commands.enter();
    },
    [editor]
  );
  return /* @__PURE__ */ jsxs(Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsx(
      ToolbarButton,
      {
        isActive: editor.isActive("link"),
        tooltip: "Link",
        "aria-label": "Insert link",
        disabled: editor.isActive("codeBlock"),
        size,
        variant,
        children: /* @__PURE__ */ jsx(Link2Icon, { className: "size-5" })
      }
    ) }),
    /* @__PURE__ */ jsx(PopoverContent, { align: "end", side: "bottom", children: /* @__PURE__ */ jsx(LinkEditBlock, { onSave: onSetLink, defaultText: text }) })
  ] });
};

export { LinkEditPopover };
