'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const popover = require('../../../popover.cjs');
const reactIcons_esm = require('../../../../../../../node_modules/.pnpm/@radix-ui_react-icons@1.3.2_react@19.2.0/node_modules/@radix-ui/react-icons/dist/react-icons.esm.cjs');
const toolbarButton = require('../toolbar-button.cjs');
const linkEditBlock = require('./link-edit-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 LinkEditPopover = ({ editor, size, variant }) => {
  const [open, setOpen] = React__namespace.useState(false);
  const { from, to } = editor.state.selection;
  const text = editor.state.doc.textBetween(from, to, " ");
  const onSetLink = React__namespace.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__ */ jsxRuntime.jsxs(popover.Popover, { open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsxRuntime.jsx(popover.PopoverTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(
      toolbarButton.ToolbarButton,
      {
        isActive: editor.isActive("link"),
        tooltip: "Link",
        "aria-label": "Insert link",
        disabled: editor.isActive("codeBlock"),
        size,
        variant,
        children: /* @__PURE__ */ jsxRuntime.jsx(reactIcons_esm.Link2Icon, { className: "size-5" })
      }
    ) }),
    /* @__PURE__ */ jsxRuntime.jsx(popover.PopoverContent, { align: "end", side: "bottom", children: /* @__PURE__ */ jsxRuntime.jsx(linkEditBlock.LinkEditBlock, { onSave: onSetLink, defaultText: text }) })
  ] });
};

exports.LinkEditPopover = LinkEditPopover;
