"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toHeading = exports.resetHeading = void 0;
const commands_1 = require("../../../pm/commands.js");
const utils_1 = require("../../../pm/utils.js");
const BaseSchema_1 = require("../../base/BaseSchema/index.js");
const HeadingSpecs_1 = require("./HeadingSpecs/index.js");
const resetHeading = (state, dispatch, view) => {
    const { selection } = state;
    if (selection.empty &&
        selection.$from.parent.type === (0, HeadingSpecs_1.headingType)(state.schema) &&
        view?.endOfTextblock('backward', state)) {
        return (0, BaseSchema_1.toParagraph)(state, dispatch, view);
    }
    return false;
};
exports.resetHeading = resetHeading;
const toHeading = (level) => (state, dispatch, view) => {
    const attrs = {};
    const parentHeading = (0, utils_1.findParentNodeOfType)((0, HeadingSpecs_1.headingType)(state.schema))(state.selection);
    if (parentHeading) {
        if (parentHeading.node.attrs[HeadingSpecs_1.headingLevelAttr] === level) {
            return (0, BaseSchema_1.toParagraph)(state, dispatch, view);
        }
        Object.assign(attrs, parentHeading.node.attrs);
    }
    attrs[HeadingSpecs_1.headingLevelAttr] = level;
    return (0, commands_1.setBlockType)((0, HeadingSpecs_1.headingType)(state.schema), attrs)(state, dispatch);
};
exports.toHeading = toHeading;
//# sourceMappingURL=commands.js.map
