"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Breaks = exports.sbType = exports.hbType = exports.BreakNodeName = exports.BreaksSpecs = void 0;
const prosemirror_commands_1 = require("prosemirror-commands");
const prosemirror_state_1 = require("prosemirror-state");
const logger_1 = require("../../../logger.js");
const platform_1 = require("../../../utils/platform.js");
const selection_1 = require("../../../utils/selection.js");
const BaseSchemaSpecs_1 = require("../../base/BaseSchema/BaseSchemaSpecs/index.js");
const BreaksSpecs_1 = require("./BreaksSpecs/index.js");
var BreaksSpecs_2 = require("./BreaksSpecs/index.js");
Object.defineProperty(exports, "BreaksSpecs", { enumerable: true, get: function () { return BreaksSpecs_2.BreaksSpecs; } });
Object.defineProperty(exports, "BreakNodeName", { enumerable: true, get: function () { return BreaksSpecs_2.BreakNodeName; } });
Object.defineProperty(exports, "hbType", { enumerable: true, get: function () { return BreaksSpecs_2.hbType; } });
Object.defineProperty(exports, "sbType", { enumerable: true, get: function () { return BreaksSpecs_2.sbType; } });
const Breaks = (builder, opts) => {
    let preferredBreak;
    if (builder.context.has('breaks')) {
        preferredBreak = builder.context.get('breaks') ? 'soft' : 'hard';
    }
    else {
        preferredBreak = opts.preferredBreak ?? 'hard';
        logger_1.globalLogger.info("[Breaks extension]: Parameter 'breaks' is not defined in context; value from options is used");
        builder.logger.log("[Breaks extension]: Parameter 'breaks' is not defined in context; value from options is used");
    }
    builder.use(BreaksSpecs_1.BreaksSpecs, { preferredBreak });
    builder.addKeymap(({ schema }) => {
        const cmd = addBr((preferredBreak === 'soft' ? BreaksSpecs_1.sbType : BreaksSpecs_1.hbType)(schema));
        const keys = {
            'Shift-Enter': cmd,
        };
        if ((0, platform_1.isMac)()) {
            keys['Ctrl-Enter'] = cmd;
        }
        return keys;
    });
};
exports.Breaks = Breaks;
const addBr = (br) => (0, prosemirror_commands_1.chainCommands)(prosemirror_commands_1.exitCode, (state, dispatch) => {
    const { selection: sel, schema } = state;
    if (!(0, selection_1.isTextSelection)(sel) ||
        !sel.empty ||
        // breaks can only be in the paragraph
        sel.$cursor?.parent.type !== (0, BaseSchemaSpecs_1.pType)(schema))
        return false;
    if (isBreakNode(sel.$cursor.nodeBefore)) {
        if (dispatch) {
            const { $cursor, $cursor: { pos }, } = sel;
            const from = isBreakNode($cursor.nodeAfter) ? pos + 1 : pos;
            const posEnd = $cursor.end();
            const posAfter = $cursor.after();
            const contentAfter = state.doc.slice(from, posEnd, false).content;
            let tr = state.tr.insert(posAfter, (0, BaseSchemaSpecs_1.pType)(schema).create(null, contentAfter));
            tr = tr
                .setSelection(prosemirror_state_1.TextSelection.create(tr.doc, posAfter + 1))
                .scrollIntoView()
                .delete(pos, posEnd) // remove content after current pos (it's moved to next para)
                .delete(pos - 1, pos); // remove break before current pos ($cursor.nodeBefore)
            dispatch(tr);
        }
        return true;
    }
    dispatch?.(state.tr.replaceSelectionWith(br.create()).scrollIntoView());
    return true;
});
function isBreakNode(node) {
    return Boolean(node?.type.spec.isBreak);
}
//# sourceMappingURL=index.js.map
