/**
 * DevExtreme (cjs/exporter/jspdf/common/height_updater.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.updateRowsAndCellsHeights = updateRowsAndCellsHeights;
var _type = require("../../../core/utils/type");
var _pdf_utils = require("./pdf_utils");

function updateRowsAndCellsHeights(doc, rows) {
    const rowsAdditionalHeights = calculateAdditionalRowsHeights(doc, rows);
    rows.forEach(row => {
        row.height += rowsAdditionalHeights[row.rowIndex]
    });
    rows.forEach(row => {
        row.cells.forEach(cell => {
            var _cell$rowSpan;
            const rowsCount = (null !== (_cell$rowSpan = cell.rowSpan) && void 0 !== _cell$rowSpan ? _cell$rowSpan : 0) + 1;
            cell.pdfCell._rect.h = rows.slice(row.rowIndex, row.rowIndex + rowsCount).reduce((accumulator, rowInfo) => accumulator + rowInfo.height, 0)
        })
    })
}

function calculateAdditionalRowsHeights(doc, rows) {
    const rowsAdditionalHeights = Array.from({
        length: rows.length
    }, () => 0);
    const sortedRows = sortRowsByMaxRowSpanAsc(rows);
    sortedRows.forEach(row => {
        const cellsWithRowSpan = row.cells.filter(cell => (0, _type.isDefined)(cell.rowSpan));
        cellsWithRowSpan.forEach(cell => {
            const targetRectWidth = (0, _pdf_utils.calculateTargetRectWidth)(cell.pdfCell._rect.w, cell.pdfCell.padding);
            const textHeight = (0, _pdf_utils.calculateTextHeight)(doc, cell.pdfCell.text, cell.pdfCell.font, {
                wordWrapEnabled: cell.pdfCell.wordWrapEnabled,
                targetRectWidth: targetRectWidth
            });
            const cellHeight = textHeight + cell.pdfCell.padding.top + cell.pdfCell.padding.bottom;
            const rowsCount = cell.rowSpan + 1;
            const currentRowSpanRowsHeight = rows.slice(row.rowIndex, row.rowIndex + rowsCount).reduce((accumulator, rowInfo) => accumulator + rowInfo.height + rowsAdditionalHeights[rowInfo.rowIndex], 0);
            if (cellHeight > currentRowSpanRowsHeight) {
                const delta = (cellHeight - currentRowSpanRowsHeight) / rowsCount;
                for (let spanIndex = row.rowIndex; spanIndex < row.rowIndex + rowsCount; spanIndex++) {
                    rowsAdditionalHeights[spanIndex] += delta
                }
            }
        })
    });
    return rowsAdditionalHeights
}

function sortRowsByMaxRowSpanAsc(rows) {
    const getMaxRowSpan = row => {
        const spansArray = row.cells.map(cell => {
            var _cell$rowSpan2;
            return null !== (_cell$rowSpan2 = cell.rowSpan) && void 0 !== _cell$rowSpan2 ? _cell$rowSpan2 : 0
        });
        return Math.max(...spansArray)
    };
    return [...rows].sort((row1, row2) => {
        const row1RowSpan = getMaxRowSpan(row1);
        const row2RowSpan = getMaxRowSpan(row2);
        if (row1RowSpan > row2RowSpan) {
            return 1
        }
        if (row2RowSpan > row1RowSpan) {
            return -1
        }
        return 0
    })
}
