"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseSchemaSpecs = exports.pType = exports.BaseNode = void 0;
const nodes_1 = require("../../../../utils/nodes.js");
const schema_1 = require("../../../../utils/schema.js");
var BaseNode;
(function (BaseNode) {
    BaseNode["Doc"] = "doc";
    BaseNode["Text"] = "text";
    BaseNode["Paragraph"] = "paragraph";
})(BaseNode || (exports.BaseNode = BaseNode = {}));
const paragraphLineNumberAttr = 'data-line';
exports.pType = (0, schema_1.nodeTypeFactory)(BaseNode.Paragraph);
const BaseSchemaSpecs = (builder, opts) => {
    builder
        .addNode(BaseNode.Doc, () => ({
        spec: {
            content: 'block+',
        },
        fromMd: { tokenSpec: { name: BaseNode.Doc, type: 'block', ignore: true } },
        toMd: () => {
            throw new Error('Unexpected toMd() call on doc node');
        },
    }))
        .addNode(BaseNode.Text, () => ({
        spec: {
            group: 'inline',
        },
        fromMd: { tokenSpec: { name: BaseNode.Text, type: 'node', ignore: true } },
        toMd: (state, node, parent) => {
            const { escapeText } = parent.type.spec;
            state.text(node.text ?? '', escapeText ?? !state.isAutolink);
        },
    }))
        .addNode(BaseNode.Paragraph, () => ({
        spec: {
            attrs: { [paragraphLineNumberAttr]: { default: null } },
            content: 'inline*',
            group: 'block',
            parseDOM: [{ tag: 'p' }],
            toDOM(node) {
                const lineNumber = node.attrs[paragraphLineNumberAttr];
                return [
                    'p',
                    lineNumber === null ? {} : { [paragraphLineNumberAttr]: lineNumber },
                    0,
                ];
            },
            selectable: true,
            placeholder: opts.paragraphPlaceholder
                ? {
                    content: opts.paragraphPlaceholder,
                    alwaysVisible: false,
                }
                : undefined,
        },
        fromMd: {
            tokenSpec: {
                name: BaseNode.Paragraph,
                type: 'block',
                getAttrs(token) {
                    return Object.fromEntries(token.attrs || []);
                },
            },
        },
        toMd: (state, node, parent) => {
            /*
                An empty line is added only if there is some content in the parent element.
                This is necessary in order to prevent an empty document with empty lines
            */
            if (opts.preserveEmptyRows && (0, nodes_1.isEmptyString)(node)) {
                let isParentEmpty = true;
                for (let index = 0; index < parent.content.childCount; index++) {
                    const parentChild = parent.content.child(index);
                    if (parentChild.content.size !== 0 ||
                        parentChild.type.name !== 'paragraph') {
                        isParentEmpty = false;
                    }
                }
                if (!isParentEmpty) {
                    state.write('&nbsp;\n');
                    state.write('\n');
                }
            }
            else {
                state.renderInline(node);
                state.closeBlock(node);
            }
        },
    }));
};
exports.BaseSchemaSpecs = BaseSchemaSpecs;
//# sourceMappingURL=index.js.map
