"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.yfmTableHeaderRowsPlugin = void 0;
const state_1 = require("../../../../../../pm/state.js");
const view_1 = require("../../../../../../pm/view.js");
const table_utils_1 = require("../../../../../../table-utils/index.js");
const table_desc_1 = require("../../../../../../table-utils/table-desc.js");
const transaction_1 = require("../../../../../../utils/transaction.js");
function buildHeaderDecorations(doc) {
    const out = [];
    doc.descendants((node, pos) => {
        if (node.isTextblock)
            return false;
        if ((0, table_utils_1.isTableNode)(node)) {
            const tableDesc = table_desc_1.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(view_1.Decoration.node(from, to, { 'data-header': 'true' }));
                }
            }
        }
        return true;
    });
    return out;
}
const yfmTableHeaderRowsPlugin = () => new state_1.Plugin({
    state: {
        init(_config, state) {
            return view_1.DecorationSet.create(state.doc, buildHeaderDecorations(state.doc));
        },
        apply(tr, set) {
            if (!tr.docChanged || (0, transaction_1.isNonStructuralTr)(tr))
                return set.map(tr.mapping, tr.doc);
            return view_1.DecorationSet.create(tr.doc, buildHeaderDecorations(tr.doc));
        },
    },
    props: {
        decorations(state) {
            return this.getState(state);
        },
    },
});
exports.yfmTableHeaderRowsPlugin = yfmTableHeaderRowsPlugin;
//# sourceMappingURL=header-rows-plugin.js.map
