import { TextSelection } from 'prosemirror-state';
import { nodeInputRule } from "../../../utils/inputrules.js";
import { get$Cursor, isNodeSelection } from "../../../utils/selection.js";
import { pType } from "../../base/BaseSchema/BaseSchemaSpecs/index.js";
import { HorizontalRuleSpecs, horizontalRuleMarkupAttr, horizontalRuleNodeName, horizontalRuleType, } from "./HorizontalRuleSpecs/index.js";
export { horizontalRuleMarkupAttr, horizontalRuleNodeName, horizontalRuleType, } from "./HorizontalRuleSpecs/index.js";
const hrAction = 'hRule';
export const HorizontalRule = (builder) => {
    builder.use(HorizontalRuleSpecs);
    builder.addInputRules((deps) => ({
        rules: [
            // --- or ___ or *** at start of line are converted to horizontal line
            // and add new empty paragraph after
            nodeInputRule(/^(---|___|\*\*\*)$/, (markup) => [
                horizontalRuleType(deps.schema).create({ [horizontalRuleMarkupAttr]: markup }),
                pType(deps.schema).create(),
            ], 1),
        ],
    }));
    builder.addAction(hrAction, ({ schema }) => {
        const cmd = addHr(horizontalRuleType(schema));
        return {
            isActive: (state) => isHrSelection(state.selection),
            isEnable: cmd,
            run: cmd,
        };
    });
};
const addHr = (hr) => (state, dispatch) => {
    if (isHrSelection(state.selection))
        return true;
    if (isNodeSelection(state.selection)) {
        dispatch?.(state.tr.replaceSelectionWith(hr.create()).scrollIntoView());
        return true;
    }
    const $cursor = 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 === 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(), pType(state.schema).create()]);
        tr.setSelection(TextSelection.create(tr.doc, pos + 2)); // set cursor to paragraph after hr
    }
    dispatch(tr.scrollIntoView());
    return true;
};
function isHrSelection(selection) {
    return isNodeSelection(selection) && selection.node.type.name === horizontalRuleNodeName;
}
//# sourceMappingURL=index.js.map
