/**
 * DevExtreme (esm/exporter/jspdf/common/rows_spliting_utils/create_on_split_multipage_row.js)
 * Version: 23.1.6 (build 23316-0110)
 * Build date: Sun Nov 12 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import {
    getTextLines,
    getTextDimensions,
    calculateTextHeight
} from "../pdf_utils";

function createMultiCellRect(rect, text, marginTop) {
    return _extends({}, rect, {
        sourceCellInfo: _extends({}, rect.sourceCellInfo, {
            text: text
        }),
        y: marginTop
    })
}
export var createOnSplitMultiPageRow = (doc, options, headerHeight, maxBottomRight) => (isFirstPage, pageRects) => {
    var currentPageRects = [];
    var nextPageRects = [];
    var maxCurrentPageHeight = 0;
    var maxNextPageHeight = 0;
    pageRects.forEach(rect => {
        var {
            w: w,
            sourceCellInfo: sourceCellInfo
        } = rect;
        var additionalHeight = !isFirstPage && options.repeatHeaders ? headerHeight : headerHeight + options.topLeft.y;
        var heightOfOneLine = getTextDimensions(doc, sourceCellInfo.text, sourceCellInfo.font).h;
        var paddingHeight = sourceCellInfo.padding.top + sourceCellInfo.padding.bottom;
        var fullPageHeight = maxBottomRight.y - additionalHeight - paddingHeight - options.margin.top;
        var possibleLinesCount = Math.floor(fullPageHeight / (heightOfOneLine * doc.getLineHeightFactor()));
        var allLines = getTextLines(doc, sourceCellInfo.text, sourceCellInfo.font, {
            wordWrapEnabled: sourceCellInfo.wordWrapEnabled,
            targetRectWidth: w
        });
        if (possibleLinesCount < allLines.length) {
            var currentPageText = allLines.slice(0, possibleLinesCount).join("\n");
            var currentPageHeight = calculateTextHeight(doc, currentPageText, sourceCellInfo.font, {
                wordWrapEnabled: sourceCellInfo.wordWrapEnabled,
                targetRectWidth: w
            });
            maxCurrentPageHeight = Math.max(maxCurrentPageHeight, currentPageHeight + paddingHeight);
            maxNextPageHeight = rect.h - currentPageHeight;
            currentPageRects.push(createMultiCellRect(rect, currentPageText, options.margin.top));
            nextPageRects.push(createMultiCellRect(rect, allLines.slice(possibleLinesCount).join("\n"), options.margin.top))
        } else {
            var _currentPageHeight = calculateTextHeight(doc, sourceCellInfo.text, sourceCellInfo.font, {
                wordWrapEnabled: sourceCellInfo.wordWrapEnabled,
                targetRectWidth: w
            });
            maxCurrentPageHeight = Math.max(maxCurrentPageHeight, _currentPageHeight + paddingHeight);
            maxNextPageHeight = Math.max(maxNextPageHeight, _currentPageHeight + paddingHeight);
            currentPageRects.push(createMultiCellRect(rect, sourceCellInfo.text, options.margin.top));
            nextPageRects.push(createMultiCellRect(rect, "", options.margin.top))
        }
    });
    currentPageRects.forEach(rect => rect.h = maxCurrentPageHeight);
    nextPageRects.forEach(rect => rect.h = maxNextPageHeight);
    return [currentPageRects, nextPageRects]
};
