"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BehaviourPlugin = void 0;
const react_1 = require("react");
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
const lexical_1 = require("lexical");
const utils_1 = require("@lexical/utils");
function BehaviourPlugin() {
    const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
    (0, react_1.useEffect)(() => {
        (0, utils_1.mergeRegister)(editor.registerCommand(lexical_1.FOCUS_COMMAND, () => {
            editor.getEditorState().read(() => {
                const lastNode = (0, lexical_1.$getRoot)().getChildren().at(-1);
                if ((0, lexical_1.$isDecoratorNode)(lastNode)) {
                    editor.update(() => {
                        const paragraphNode = (0, lexical_1.$createParagraphNode)();
                        (0, lexical_1.$getRoot)().append(paragraphNode);
                        paragraphNode.selectStart();
                    });
                }
            });
            return true;
        }, lexical_1.COMMAND_PRIORITY_EDITOR), editor.registerCommand(lexical_1.KEY_ARROW_LEFT_COMMAND, (event) => {
            const selection = (0, lexical_1.$getSelection)();
            if (!(0, lexical_1.$isNodeSelection)(selection)) {
                return false;
            }
            const selectedNodes = selection.getNodes();
            const firstNode = selectedNodes[0];
            const topLevelElement = firstNode.getTopLevelElement();
            const previousSibling = topLevelElement.getPreviousSibling();
            if ((0, lexical_1.$isDecoratorNode)(previousSibling)) {
                event.preventDefault();
                const nodeSelection = (0, lexical_1.$createNodeSelection)();
                nodeSelection.add(previousSibling.getKey());
                (0, lexical_1.$setSelection)(nodeSelection);
                return true;
            }
            if (!previousSibling) {
                event.preventDefault();
                const paragraphNode = (0, lexical_1.$createParagraphNode)();
                topLevelElement.insertBefore(paragraphNode);
                paragraphNode.select();
                return true;
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_ARROW_RIGHT_COMMAND, (event) => {
            const selection = (0, lexical_1.$getSelection)();
            if (!(0, lexical_1.$isNodeSelection)(selection)) {
                return false;
            }
            const selectedNodes = selection.getNodes();
            const lastNode = selectedNodes[selectedNodes.length - 1];
            const topLevelElement = lastNode.getTopLevelElement();
            const nextSibling = topLevelElement.getNextSibling();
            if ((0, lexical_1.$isDecoratorNode)(nextSibling)) {
                event.preventDefault();
                const nodeSelection = (0, lexical_1.$createNodeSelection)();
                nodeSelection.add(nextSibling.getKey());
                (0, lexical_1.$setSelection)(nodeSelection);
                return true;
            }
            if (!nextSibling) {
                event.preventDefault();
                const paragraphNode = (0, lexical_1.$createParagraphNode)();
                topLevelElement.insertAfter(paragraphNode);
                paragraphNode.select();
                return true;
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_ARROW_UP_COMMAND, (event) => {
            const selection = (0, lexical_1.$getSelection)();
            if ((0, lexical_1.$isRangeSelection)(selection)) {
                if (selection.isCollapsed()) {
                    const topLevelElement = selection.anchor
                        .getNode()
                        .getTopLevelElement();
                    const previousSibling = topLevelElement?.getPreviousSibling();
                    if ((0, lexical_1.$isDecoratorNode)(previousSibling)) {
                        event.preventDefault();
                        const nodeSelection = (0, lexical_1.$createNodeSelection)();
                        nodeSelection.add(previousSibling.getKey());
                        (0, lexical_1.$setSelection)(nodeSelection);
                        return true;
                    }
                }
            }
            if ((0, lexical_1.$isNodeSelection)(selection)) {
                const selectedNodes = selection.getNodes();
                const firstNode = selectedNodes[0];
                const topLevelElement = firstNode.getTopLevelElement();
                const previousSibling = topLevelElement.getPreviousSibling();
                if ((0, lexical_1.$isDecoratorNode)(previousSibling)) {
                    event.preventDefault();
                    const nodeSelection = (0, lexical_1.$createNodeSelection)();
                    nodeSelection.add(previousSibling.getKey());
                    (0, lexical_1.$setSelection)(nodeSelection);
                    return true;
                }
                if (!previousSibling) {
                    event.preventDefault();
                    const paragraphNode = (0, lexical_1.$createParagraphNode)();
                    topLevelElement.insertBefore(paragraphNode);
                    paragraphNode.select();
                    return true;
                }
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_ARROW_DOWN_COMMAND, (event) => {
            const selection = (0, lexical_1.$getSelection)();
            if ((0, lexical_1.$isRangeSelection)(selection)) {
                if (selection.isCollapsed()) {
                    const topLevelElement = selection.anchor
                        .getNode()
                        .getTopLevelElement();
                    const nextSibling = topLevelElement?.getNextSibling();
                    if ((0, lexical_1.$isDecoratorNode)(nextSibling)) {
                        event.preventDefault();
                        const nodeSelection = (0, lexical_1.$createNodeSelection)();
                        nodeSelection.add(nextSibling.getKey());
                        (0, lexical_1.$setSelection)(nodeSelection);
                        return true;
                    }
                }
            }
            if ((0, lexical_1.$isNodeSelection)(selection)) {
                const selectedNodes = selection.getNodes();
                const lastNode = selectedNodes[selectedNodes.length - 1];
                const topLevelElement = lastNode.getTopLevelElement();
                const nextSibling = topLevelElement.getNextSibling();
                if ((0, lexical_1.$isDecoratorNode)(nextSibling)) {
                    event.preventDefault();
                    const nodeSelection = (0, lexical_1.$createNodeSelection)();
                    nodeSelection.add(nextSibling.getKey());
                    (0, lexical_1.$setSelection)(nodeSelection);
                    return true;
                }
                if (!nextSibling) {
                    event.preventDefault();
                    const paragraphNode = (0, lexical_1.$createParagraphNode)();
                    topLevelElement.insertAfter(paragraphNode);
                    paragraphNode.select();
                    return true;
                }
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_ENTER_COMMAND, (event) => {
            const selection = (0, lexical_1.$getSelection)();
            if ((0, lexical_1.$isNodeSelection)(selection)) {
                event.preventDefault();
                const selectedNodes = selection.getNodes();
                const lastNode = selectedNodes[selectedNodes.length - 1];
                const topLevelElement = lastNode.getTopLevelElement();
                const paragraphNode = (0, lexical_1.$createParagraphNode)();
                topLevelElement.insertAfter(paragraphNode);
                paragraphNode.select();
                return true;
            }
            return false;
        }, lexical_1.COMMAND_PRIORITY_LOW));
    }, [editor]);
    return null;
}
exports.BehaviourPlugin = BehaviourPlugin;
//# sourceMappingURL=BehaviourPlugin.js.map