import { Plugin } from "../../../../../../pm/state.js";
import { Decoration, DecorationSet } from "../../../../../../pm/view.js";
import { isTableNode } from "../../../../../../table-utils/index.js";
import { TableDesc } from "../../../../../../table-utils/table-desc.js";
import { isNonStructuralTr } from "../../../../../../utils/transaction.js";
function buildHeaderDecorations(doc) {
    const out = [];
    doc.descendants((node, pos) => {
        if (node.isTextblock)
            return false;
        if (isTableNode(node)) {
            const tableDesc = TableDesc.create(node);
            if (tableDesc?.headerRows) {
                const bound = tableDesc.bind(pos);
                for (let i = 0; i < tableDesc.headerRows; i++) {
                    const { from, to } = bound.getPosForRow(i);
                    out.push(Decoration.node(from, to, { 'data-header': 'true' }));
                }
            }
        }
        return true;
    });
    return out;
}
export const yfmTableHeaderRowsPlugin = () => new Plugin({
    state: {
        init(_config, state) {
            return DecorationSet.create(state.doc, buildHeaderDecorations(state.doc));
        },
        apply(tr, set) {
            if (!tr.docChanged || isNonStructuralTr(tr))
                return set.map(tr.mapping, tr.doc);
            return DecorationSet.create(tr.doc, buildHeaderDecorations(tr.doc));
        },
    },
    props: {
        decorations(state) {
            return this.getState(state);
        },
    },
});
//# sourceMappingURL=header-rows-plugin.js.map
