"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toggleFoldingOfHeading = void 0;
const specs_1 = require("../../../base/specs.js");
const const_1 = require("../const.js");
const utils_1 = require("../utils.js");
const toggleFoldingOfHeading = (state, dispatch) => {
    if (!(0, utils_1.insideHeading)(state.selection))
        return false;
    if (dispatch) {
        const { $head } = state.selection;
        const folding = (0, utils_1.isFoldingHeading)($head.parent);
        const level = (0, utils_1.parseLevel)($head.parent);
        const tr = state.tr.setNodeAttribute($head.before(), const_1.YfmHeadingAttr.Folding, folding ? null : false);
        if (!folding) {
            // insert empty paragraph if content of new folding heading is empty
            const nextNode = $head.node(-1).maybeChild($head.indexAfter(-1));
            if (!nextNode || ((0, utils_1.isFoldingHeading)(nextNode) && (0, utils_1.parseLevel)(nextNode) <= level)) {
                tr.insert($head.after(), (0, specs_1.pType)(state.schema).create());
            }
        }
        dispatch(tr);
    }
    return true;
};
exports.toggleFoldingOfHeading = toggleFoldingOfHeading;
//# sourceMappingURL=toggleFoldingOfHeading.js.map
