"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.setCodeBlockType = exports.resetCodeblock = exports.newlineInCode = void 0;
const prosemirror_commands_1 = require("prosemirror-commands");
Object.defineProperty(exports, "newlineInCode", { enumerable: true, get: function () { return prosemirror_commands_1.newlineInCode; } });
const BaseSchema_1 = require("../../base/BaseSchema/index.js");
const const_1 = require("./const.js");
const resetCodeblock = (state, dispatch, view) => {
    const { selection } = state;
    if (selection.empty &&
        selection.$from.parent.type === (0, const_1.codeBlockType)(state.schema) &&
        view?.endOfTextblock('backward', state)) {
        return (0, BaseSchema_1.toParagraph)(state, dispatch, view);
    }
    return false;
};
exports.resetCodeblock = resetCodeblock;
const setCodeBlockType = ({ serializer }) => (state, dispatch) => {
    const nodeType = (0, const_1.codeBlockType)(state.schema);
    if (!(0, prosemirror_commands_1.setBlockType)(nodeType)(state))
        return false;
    if (dispatch) {
        // FIXME: Verify and use Node instead of Fragment
        const markup = serializer.serialize(state.selection.content().content, {
            escape: false,
        });
        dispatch(state.tr.replaceSelectionWith(nodeType.createAndFill({}, markup ? state.schema.text(markup) : null)));
    }
    return true;
};
exports.setCodeBlockType = setCodeBlockType;
//# sourceMappingURL=commands.js.map
