import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { DEFAULT_LINK_PROTOCOL, VALID_LINK_PROTOCOLS } from "@endexai/blocknote-core";
import { useCallback, useEffect, useState, } from "react";
import { RiLink, RiText } from "react-icons/ri";
import { useComponentsContext } from "../../editor/ComponentsContext.js";
import { useDictionary } from "../../i18n/dictionary.js";
const validateUrl = (url) => {
    for (const protocol of VALID_LINK_PROTOCOLS) {
        if (url.startsWith(protocol)) {
            return url;
        }
    }
    return `${DEFAULT_LINK_PROTOCOL}://${url}`;
};
export const EditLinkMenuItems = (props) => {
    const Components = useComponentsContext();
    const dict = useDictionary();
    const { url, text, editLink } = props;
    const [currentUrl, setCurrentUrl] = useState(url);
    const [currentText, setCurrentText] = useState(text);
    useEffect(() => {
        setCurrentUrl(url);
        setCurrentText(text);
    }, [text, url]);
    const handleEnter = useCallback((event) => {
        if (event.key === "Enter") {
            event.preventDefault();
            editLink(validateUrl(currentUrl), currentText);
        }
    }, [editLink, currentUrl, currentText]);
    const handleUrlChange = useCallback((event) => setCurrentUrl(event.currentTarget.value), []);
    const handleTextChange = useCallback((event) => setCurrentText(event.currentTarget.value), []);
    const handleSubmit = useCallback(() => editLink(validateUrl(currentUrl), currentText), [editLink, currentUrl, currentText]);
    return (_jsxs(Components.Generic.Form.Root, { children: [_jsx(Components.Generic.Form.TextInput, { className: "bn-text-input", name: "url", icon: _jsx(RiLink, {}), autoFocus: true, placeholder: dict.link_toolbar.form.url_placeholder, value: currentUrl, onKeyDown: handleEnter, onChange: handleUrlChange, onSubmit: handleSubmit }), _jsx(Components.Generic.Form.TextInput, { className: "bn-text-input", name: "title", icon: _jsx(RiText, {}), placeholder: dict.link_toolbar.form.title_placeholder, value: currentText, onKeyDown: handleEnter, onChange: handleTextChange, onSubmit: handleSubmit })] }));
};
//# sourceMappingURL=EditLinkMenuItems.js.map