import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useEffect, useMemo, useState } from "react";
import { RiLink } from "react-icons/ri";
import { formatKeyboardShortcut, } from "@endexai/blocknote-core";
import { useComponentsContext } from "../../../editor/ComponentsContext.js";
import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js";
import { useEditorContentOrSelectionChange } from "../../../hooks/useEditorContentOrSelectionChange.js";
import { useSelectedBlocks } from "../../../hooks/useSelectedBlocks.js";
import { useDictionary } from "../../../i18n/dictionary.js";
import { EditLinkMenuItems } from "../../LinkToolbar/EditLinkMenuItems.js";
function checkLinkInSchema(editor) {
    return ("link" in editor.schema.inlineContentSchema &&
        editor.schema.inlineContentSchema["link"] === "link");
}
export const CreateLinkButton = () => {
    const editor = useBlockNoteEditor();
    const Components = useComponentsContext();
    const dict = useDictionary();
    const linkInSchema = checkLinkInSchema(editor);
    const selectedBlocks = useSelectedBlocks(editor);
    const [opened, setOpened] = useState(false);
    const [url, setUrl] = useState(editor.getSelectedLinkUrl() || "");
    const [text, setText] = useState(editor.getSelectedText());
    useEditorContentOrSelectionChange(() => {
        setOpened(false);
        setText(editor.getSelectedText() || "");
        setUrl(editor.getSelectedLinkUrl() || "");
    }, editor);
    useEffect(() => {
        const callback = (event) => {
            if ((event.ctrlKey || event.metaKey) && event.key === "k") {
                setOpened(true);
                event.preventDefault();
            }
        };
        editor.prosemirrorView?.dom.addEventListener("keydown", callback);
        return () => {
            editor.prosemirrorView?.dom.removeEventListener("keydown", callback);
        };
    }, [editor.prosemirrorView?.dom]);
    const update = useCallback((url, text) => {
        editor.createLink(url, text);
        editor.focus();
    }, [editor]);
    const show = useMemo(() => {
        if (!linkInSchema) {
            return false;
        }
        for (const block of selectedBlocks) {
            if (block.content === undefined) {
                return false;
            }
        }
        return true;
    }, [linkInSchema, selectedBlocks]);
    if (!show ||
        !("link" in editor.schema.inlineContentSchema) ||
        !editor.isEditable) {
        return null;
    }
    return (_jsxs(Components.Generic.Popover.Root, { opened: opened, children: [_jsx(Components.Generic.Popover.Trigger, { children: _jsx(Components.FormattingToolbar.Button, { className: "bn-button", "data-test": "createLink", label: dict.formatting_toolbar.link.tooltip, mainTooltip: dict.formatting_toolbar.link.tooltip, secondaryTooltip: formatKeyboardShortcut(dict.formatting_toolbar.link.secondary_tooltip, dict.generic.ctrl_shortcut), icon: _jsx(RiLink, {}), onClick: () => setOpened(true) }) }), _jsx(Components.Generic.Popover.Content, { className: "bn-popover-content bn-form-popover", variant: "form-popover", children: _jsx(EditLinkMenuItems, { url: url, text: text, editLink: update }) })] }));
};
//# sourceMappingURL=CreateLinkButton.js.map