"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializerTokens = void 0;
const tslib_1 = require("tslib");
const is_number_1 = tslib_1.__importDefault(require("is-number"));
const const_1 = require("./const.js");
exports.serializerTokens = {
    [const_1.YfmTableNode.Table]: (state, node) => {
        state.ensureNewLine();
        state.write('#|');
        state.ensureNewLine();
        state.renderContent(node);
        state.write('|#');
        state.ensureNewLine();
        state.closeBlock();
        state.write('\n');
    },
    [const_1.YfmTableNode.Body]: (state, tbody) => {
        const rowspanStack = {};
        tbody.forEach((trow) => {
            state.write('||');
            state.ensureNewLine();
            state.write('\n');
            let colIndex = 0;
            trow.forEach((td) => {
                while (colIndex in rowspanStack && rowspanStack[colIndex] > 0) {
                    state.write(colIndex === 0 ? '^' : '|^');
                    rowspanStack[colIndex]--;
                    colIndex++;
                }
                let rowspan = -1;
                if ((0, is_number_1.default)(td.attrs[const_1.YfmTableAttr.Rowspan])) {
                    rowspan = Math.max(0, Number.parseInt(td.attrs[const_1.YfmTableAttr.Rowspan], 10));
                    rowspanStack[colIndex] = rowspan - 1;
                }
                if (colIndex > 0) {
                    state.write('|');
                    state.ensureNewLine();
                    state.write('\n');
                }
                state.renderContent(td);
                if (td.attrs[const_1.YfmTableAttr.CellAlign]) {
                    state.write(`{.${td.attrs[const_1.YfmTableAttr.CellAlign]}}`);
                    state.ensureNewLine();
                }
                colIndex++;
                if ((0, is_number_1.default)(td.attrs[const_1.YfmTableAttr.Colspan])) {
                    let colspan = Math.max(0, Number.parseInt(td.attrs[const_1.YfmTableAttr.Colspan], 10));
                    while (--colspan > 0) {
                        state.write('|>');
                        if (rowspan > 0)
                            rowspanStack[colIndex] = rowspan - 1;
                        colIndex++;
                    }
                }
                const isLastCell = trow.lastChild === td;
                if (isLastCell) {
                    while (colIndex in rowspanStack && rowspanStack[colIndex] > 0) {
                        state.write('|^');
                        rowspanStack[colIndex]--;
                        colIndex++;
                    }
                }
            });
            state.ensureNewLine();
            state.write('||');
            state.ensureNewLine();
        });
    },
    [const_1.YfmTableNode.Row]: (state, node) => {
        console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
        state.write('||');
        state.ensureNewLine();
        state.write('\n');
        state.renderContent(node);
        state.write('||');
        state.ensureNewLine();
    },
    [const_1.YfmTableNode.Cell]: (state, node, parent) => {
        console.warn(`Should not serialize ${node.type.name} node via serialize-token`);
        state.renderContent(node);
        const isLastCellInRow = parent.lastChild === node;
        if (!isLastCellInRow) {
            state.write('|');
            state.ensureNewLine();
            state.write('\n');
        }
    },
};
//# sourceMappingURL=serializer.js.map
