import React from 'react';
import {$createButtonNode, ButtonNode, INSERT_BUTTON_COMMAND} from '../nodes/ButtonNode';
import {COMMAND_PRIORITY_LOW} from 'lexical';
import {INSERT_CARD_COMMAND} from './KoenigBehaviourPlugin';
import {mergeRegister} from '@lexical/utils';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';

export const ButtonPlugin = () => {
    const [editor] = useLexicalComposerContext();

    React.useEffect(() => {
        if (!editor.hasNodes([ButtonNode])){
            console.error('ButtonPlugin: ButtonNode not registered');
            return;
        }
        return mergeRegister(
            editor.registerCommand(
                INSERT_BUTTON_COMMAND,
                async (dataset) => {
                    const cardNode = $createButtonNode(dataset);
                    editor.dispatchCommand(INSERT_CARD_COMMAND, {cardNode, openInEditMode: true});

                    return true;
                },
                COMMAND_PRIORITY_LOW
            )
        );
    }, [editor]);

    return null;
};

export default ButtonPlugin;
