"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.backToNoteTitle = exports.removeNote = exports.liftEmptyBlockFromNote = exports.exitFromNoteTitle = void 0;
const prosemirror_model_1 = require("prosemirror-model");
const prosemirror_state_1 = require("prosemirror-state");
const nodes_1 = require("../../../utils/nodes.js");
const schema_1 = require("../../../utils/schema.js");
const selection_1 = require("../../../utils/selection.js");
const base_1 = require("../../base/index.js");
const utils_1 = require("./utils.js");
const exitFromNoteTitle = (state, dispatch) => {
    const { selection, schema } = state;
    if (!(0, selection_1.isTextSelection)(selection))
        return false;
    const { $cursor } = selection;
    if (!$cursor)
        return false;
    if (!(0, schema_1.isSameNodeType)($cursor.parent, (0, utils_1.noteTitleType)(schema)) ||
        !(0, schema_1.isSameNodeType)($cursor.node(-1), (0, utils_1.noteType)(schema))) {
        return false;
    }
    const noteNode = $cursor.node(-1);
    const noteContentFirstTextblockChild = (0, nodes_1.findFirstTextblockChild)(getNoteContent(noteNode));
    if (!noteContentFirstTextblockChild)
        return false;
    const noteContentPos = $cursor.start(-1) + noteNode.firstChild.nodeSize;
    const targetPos = noteContentPos + noteContentFirstTextblockChild.offset;
    dispatch?.(state.tr.setSelection(prosemirror_state_1.TextSelection.create(state.doc, targetPos)));
    return true;
};
exports.exitFromNoteTitle = exitFromNoteTitle;
const liftEmptyBlockFromNote = (state, dispatch) => {
    const { selection, schema } = state;
    if (!(0, selection_1.isTextSelection)(selection))
        return false;
    const { $cursor } = selection;
    // cursor should be inside an empty textblock
    if (!$cursor || $cursor.parent.content.size)
        return false;
    if ($cursor.depth > 2 && // depth must be at least 3
        (0, schema_1.isSameNodeType)($cursor.node(-1), (0, utils_1.noteContentType)(schema)) &&
        (0, schema_1.isSameNodeType)($cursor.node(-2), (0, utils_1.noteType)(schema)) &&
        $cursor.node(-1).childCount > 1) {
        // current texblock is last child
        if ($cursor.after() === $cursor.end(-1)) {
            if (dispatch) {
                dispatch(state.tr.lift($cursor.blockRange(), $cursor.depth - 3).scrollIntoView());
            }
            return true;
        }
    }
    return false;
};
exports.liftEmptyBlockFromNote = liftEmptyBlockFromNote;
function getNoteContent(noteNode) {
    const noteChildren = [];
    noteNode.forEach((node) => noteChildren.push(node));
    // first child node is note title
    return prosemirror_model_1.Fragment.from(noteChildren.slice(1));
}
/**
 * Deletes the note if the cursor is at the beginning of the title.
 * Note will be replaced with his title converted to paragraph and his content.
 */
const removeNote = (state, dispatch, view) => {
    const { selection, schema } = state;
    if (!(0, selection_1.isTextSelection)(selection))
        return false;
    const { $cursor } = selection;
    if (!$cursor)
        return false;
    if (!(0, schema_1.isSameNodeType)($cursor.parent, (0, utils_1.noteTitleType)(schema)) ||
        !(0, schema_1.isSameNodeType)($cursor.node(-1), (0, utils_1.noteType)(schema))) {
        return false;
    }
    if (view?.endOfTextblock('backward', state)) {
        if (dispatch) {
            const noteTitleNode = $cursor.parent;
            const noteNode = $cursor.node(-1);
            const noteContentNode = noteNode.lastChild;
            const content = prosemirror_model_1.Fragment.from((0, base_1.pType)(schema).create(null, noteTitleNode.content)).append(noteContentNode.content);
            const from = $cursor.before(-1);
            const to = from + noteNode.nodeSize;
            const tr = state.tr.replaceWith(from, to, content);
            dispatch(tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, from + 1)));
        }
        return true;
    }
    return false;
};
exports.removeNote = removeNote;
/**
 * If the cursor is at the beginning of the first paragraph in note's content,
 * moves the cursor to the end of the note's title.
 */
const backToNoteTitle = (state, dispatch, view) => {
    const { selection, schema } = state;
    if (!(0, selection_1.isTextSelection)(selection))
        return false;
    const { $cursor } = selection;
    if (!$cursor)
        return false;
    if (!(0, schema_1.isSameNodeType)($cursor.parent, (0, base_1.pType)(schema)) ||
        !(0, schema_1.isSameNodeType)($cursor.node(-1), (0, utils_1.noteContentType)(schema)) ||
        !(0, schema_1.isSameNodeType)($cursor.node(-2), (0, utils_1.noteType)(schema))) {
        return false;
    }
    const noteNode = $cursor.node(-2);
    const noteContentNode = $cursor.node(-1);
    if ($cursor.parent !== noteContentNode.firstChild)
        return false;
    if (view?.endOfTextblock('backward', state)) {
        if (dispatch) {
            const noteTitleNode = noteNode.firstChild;
            dispatch(state.tr.setSelection(prosemirror_state_1.TextSelection.create(state.doc, $cursor.before(-2) + noteTitleNode.nodeSize)));
        }
        return true;
    }
    return false;
};
exports.backToNoteTitle = backToNoteTitle;
//# sourceMappingURL=commands.js.map
