"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HorizontalRule = exports.horizontalRuleType = exports.horizontalRuleNodeName = exports.horizontalRuleMarkupAttr = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const inputrules_1 = require("../../../utils/inputrules.js");
const selection_1 = require("../../../utils/selection.js");
const BaseSchemaSpecs_1 = require("../../base/BaseSchema/BaseSchemaSpecs/index.js");
const HorizontalRuleSpecs_1 = require("./HorizontalRuleSpecs/index.js");
var HorizontalRuleSpecs_2 = require("./HorizontalRuleSpecs/index.js");
Object.defineProperty(exports, "horizontalRuleMarkupAttr", { enumerable: true, get: function () { return HorizontalRuleSpecs_2.horizontalRuleMarkupAttr; } });
Object.defineProperty(exports, "horizontalRuleNodeName", { enumerable: true, get: function () { return HorizontalRuleSpecs_2.horizontalRuleNodeName; } });
Object.defineProperty(exports, "horizontalRuleType", { enumerable: true, get: function () { return HorizontalRuleSpecs_2.horizontalRuleType; } });
const hrAction = 'hRule';
const HorizontalRule = (builder) => {
    builder.use(HorizontalRuleSpecs_1.HorizontalRuleSpecs);
    builder.addInputRules((deps) => ({
        rules: [
            // --- or ___ or *** at start of line are converted to horizontal line
            // and add new empty paragraph after
            (0, inputrules_1.nodeInputRule)(/^(---|___|\*\*\*)$/, (markup) => [
                (0, HorizontalRuleSpecs_1.horizontalRuleType)(deps.schema).create({ [HorizontalRuleSpecs_1.horizontalRuleMarkupAttr]: markup }),
                (0, BaseSchemaSpecs_1.pType)(deps.schema).create(),
            ], 1),
        ],
    }));
    builder.addAction(hrAction, ({ schema }) => {
        const cmd = addHr((0, HorizontalRuleSpecs_1.horizontalRuleType)(schema));
        return {
            isActive: (state) => isHrSelection(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    });
};
exports.HorizontalRule = HorizontalRule;
const addHr = (hr) => (state, dispatch) => {
    if (isHrSelection(state.selection))
        return true;
    if ((0, selection_1.isNodeSelection)(state.selection)) {
        dispatch?.(state.tr.replaceSelectionWith(hr.create()).scrollIntoView());
        return true;
    }
    const $cursor = (0, selection_1.get$Cursor)(state.selection);
    if (!$cursor || $cursor.parent.type.spec.complex)
        return false;
    if (!dispatch)
        return true;
    const { tr } = state;
    const isEmptyParagraph = $cursor.parent.type === (0, BaseSchemaSpecs_1.pType)(state.schema) && $cursor.parent.nodeSize === 2;
    if (isEmptyParagraph) {
        const pos = $cursor.before();
        tr.insert(pos, hr.create());
    }
    else {
        const pos = $cursor.after();
        tr.insert(pos, [hr.create(), (0, BaseSchemaSpecs_1.pType)(state.schema).create()]);
        tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, pos + 2)); // set cursor to paragraph after hr
    }
    dispatch(tr.scrollIntoView());
    return true;
};
function isHrSelection(selection) {
    return (0, selection_1.isNodeSelection)(selection) && selection.node.type.name === HorizontalRuleSpecs_1.horizontalRuleNodeName;
}
//# sourceMappingURL=index.js.map
