import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { checkBlockIsFileBlock, checkBlockIsFileBlockWithPlaceholder, } from "@endexai/blocknote-core";
import { useCallback, useMemo, useState, } from "react";
import { RiFontFamily } from "react-icons/ri";
import { useComponentsContext } from "../../../editor/ComponentsContext.js";
import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js";
import { useSelectedBlocks } from "../../../hooks/useSelectedBlocks.js";
import { useDictionary } from "../../../i18n/dictionary.js";
export const FileRenameButton = () => {
    const dict = useDictionary();
    const Components = useComponentsContext();
    const editor = useBlockNoteEditor();
    const [currentEditingName, setCurrentEditingName] = useState();
    const selectedBlocks = useSelectedBlocks(editor);
    const fileBlock = useMemo(() => {
        // Checks if only one block is selected.
        if (selectedBlocks.length !== 1) {
            return undefined;
        }
        const block = selectedBlocks[0];
        if (checkBlockIsFileBlock(block, editor)) {
            setCurrentEditingName(block.props.name);
            return block;
        }
        return undefined;
    }, [editor, selectedBlocks]);
    const handleEnter = useCallback((event) => {
        if (fileBlock && event.key === "Enter") {
            event.preventDefault();
            editor.updateBlock(fileBlock, {
                props: {
                    name: currentEditingName, // TODO
                },
            });
        }
    }, [currentEditingName, editor, fileBlock]);
    const handleChange = useCallback((event) => setCurrentEditingName(event.currentTarget.value), []);
    if (!fileBlock ||
        checkBlockIsFileBlockWithPlaceholder(fileBlock, editor) ||
        !editor.isEditable) {
        return null;
    }
    return (_jsxs(Components.Generic.Popover.Root, { children: [_jsx(Components.Generic.Popover.Trigger, { children: _jsx(Components.FormattingToolbar.Button, { className: "bn-button", label: dict.formatting_toolbar.file_rename.tooltip[fileBlock.type] ||
                        dict.formatting_toolbar.file_rename.tooltip["file"], mainTooltip: dict.formatting_toolbar.file_rename.tooltip[fileBlock.type] ||
                        dict.formatting_toolbar.file_rename.tooltip["file"], icon: _jsx(RiFontFamily, {}) }) }), _jsx(Components.Generic.Popover.Content, { className: "bn-popover-content bn-form-popover", variant: "form-popover", children: _jsx(Components.Generic.Form.Root, { children: _jsx(Components.Generic.Form.TextInput, { name: "file-name", icon: _jsx(RiFontFamily, {}), value: currentEditingName || "", autoFocus: true, placeholder: dict.formatting_toolbar.file_rename.input_placeholder[fileBlock.type] || dict.formatting_toolbar.file_rename.input_placeholder["file"], onKeyDown: handleEnter, onChange: handleChange }) }) })] }));
};
//# sourceMappingURL=FileRenameButton.js.map