"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addParagraphToEnd = exports.addParagraphToStart = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const base_1 = require("../../base/index.js");
const addParagraphToStart = (state, dispatch) => {
    const { firstChild } = state.doc;
    if (firstChild?.type === (0, base_1.pType)(state.schema)) {
        // paragraph already at start of doc
        if (dispatch) {
            const pos = 1;
            const tr = state.tr;
            tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, pos));
            dispatch(tr.scrollIntoView());
        }
        return true;
    }
    insertParagraph(0, state, dispatch);
    return true;
};
exports.addParagraphToStart = addParagraphToStart;
const addParagraphToEnd = (state, dispatch) => {
    const { lastChild } = state.doc;
    if (!lastChild)
        return false;
    if (lastChild.type === (0, base_1.pType)(state.schema) && lastChild.nodeSize === 2) {
        // empty paragraph already at end of doc
        if (dispatch) {
            const pos = state.doc.nodeSize - 3;
            const tr = state.tr;
            tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, pos));
            dispatch(tr.scrollIntoView());
        }
        return true;
    }
    insertParagraph(state.doc.nodeSize - 2, state, dispatch);
    return true;
};
exports.addParagraphToEnd = addParagraphToEnd;
function insertParagraph(pos, state, dispatch) {
    if (!dispatch)
        return;
    const tr = state.tr;
    tr.insert(pos, (0, base_1.pType)(state.schema).create());
    tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, pos + 1));
    dispatch(tr.scrollIntoView());
}
//# sourceMappingURL=commands.js.map
