/**
 * DevExtreme (cjs/exporter/jspdf/common/pdf_utils.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.applyRtl = applyRtl;
exports.applyWordWrap = applyWordWrap;
exports.calculateRowHeight = calculateRowHeight;
exports.calculateTargetRectWidth = calculateTargetRectWidth;
exports.calculateTextHeight = calculateTextHeight;
exports.getPageHeight = getPageHeight;
exports.getPageWidth = getPageWidth;
exports.getTextDimensions = getTextDimensions;
exports.getTextLines = getTextLines;
exports.toPdfUnit = toPdfUnit;
var _type = require("../../../core/utils/type");
const DOTS_TEXT = "...";

function toPdfUnit(doc, value) {
    const coefficient = 1 / doc.internal.scaleFactor;
    return value * coefficient
}

function getPageWidth(doc) {
    return doc.internal.pageSize.getWidth()
}

function getPageHeight(doc) {
    return doc.internal.pageSize.getHeight()
}

function getTextLines(doc, text, font, _ref) {
    let {
        wordWrapEnabled: wordWrapEnabled,
        targetRectWidth: targetRectWidth
    } = _ref;
    if (wordWrapEnabled) {
        const usedFont = doc.getFont(null === font || void 0 === font ? void 0 : font.name, null === font || void 0 === font ? void 0 : font.style);
        return doc.splitTextToSize(text, targetRectWidth, {
            fontSize: (null === font || void 0 === font ? void 0 : font.size) || doc.getFontSize(),
            fontName: usedFont.fontName,
            fontStyle: usedFont.fontStyle
        })
    }
    let textWithoutLineBreak = text.split("\n").filter((ch => "" !== ch)).join(" ");
    if (getTextDimensions(doc, textWithoutLineBreak, font).w <= targetRectWidth) {
        return [textWithoutLineBreak]
    }
    let textWidth = getTextDimensions(doc, textWithoutLineBreak + DOTS_TEXT, font).w;
    while (textWithoutLineBreak.length > 0 && textWidth > targetRectWidth) {
        let symbolsCountToRemove = 0;
        if (textWidth >= 2 * targetRectWidth) {
            symbolsCountToRemove = textWithoutLineBreak.length / 2
        }
        if (symbolsCountToRemove < 1) {
            symbolsCountToRemove = 1
        }
        textWithoutLineBreak = textWithoutLineBreak.substring(0, textWithoutLineBreak.length - symbolsCountToRemove);
        textWidth = getTextDimensions(doc, textWithoutLineBreak + DOTS_TEXT, font).w
    }
    return [textWithoutLineBreak + DOTS_TEXT]
}

function calculateTargetRectWidth(columnWidth, padding) {
    const width = columnWidth - (padding.left + padding.right);
    return width >= 0 ? width : 0
}

function getTextDimensions(doc, text, font) {
    return doc.getTextDimensions(text, {
        font: doc.getFont(null === font || void 0 === font ? void 0 : font.name, null === font || void 0 === font ? void 0 : font.style),
        fontSize: (null === font || void 0 === font ? void 0 : font.size) || doc.getFontSize()
    })
}

function calculateTextHeight(doc, text, font, _ref2) {
    let {
        wordWrapEnabled: wordWrapEnabled,
        targetRectWidth: targetRectWidth
    } = _ref2;
    const heightOfOneLine = getTextDimensions(doc, text, font).h;
    const linesCount = getTextLines(doc, text, font, {
        wordWrapEnabled: wordWrapEnabled,
        targetRectWidth: targetRectWidth
    }).length;
    return heightOfOneLine * linesCount * doc.getLineHeightFactor()
}

function calculateRowHeight(doc, cells, columnWidths) {
    if (cells.length !== columnWidths.length) {
        throw "the cells count must be equal to the count of the columns"
    }
    let rowHeight = 0;
    for (let cellIndex = 0; cellIndex < cells.length; cellIndex++) {
        if ((0, _type.isDefined)(cells[cellIndex].rowSpan)) {
            continue
        }
        const cellText = cells[cellIndex].pdfCell.text;
        const cellPadding = cells[cellIndex].pdfCell.padding;
        const font = cells[cellIndex].pdfCell.font;
        const wordWrapEnabled = cells[cellIndex].pdfCell.wordWrapEnabled;
        const columnWidth = columnWidths[cellIndex];
        const targetRectWidth = calculateTargetRectWidth(columnWidth, cellPadding);
        if ((0, _type.isDefined)(cellText)) {
            const textHeight = "" !== cellText ? calculateTextHeight(doc, cellText, font, {
                wordWrapEnabled: wordWrapEnabled,
                targetRectWidth: targetRectWidth
            }) : 0;
            const cellHeight = textHeight + cellPadding.top + cellPadding.bottom;
            if (rowHeight < cellHeight) {
                rowHeight = cellHeight
            }
        }
    }
    return rowHeight
}

function applyWordWrap(doc, rowsInfo) {
    rowsInfo.forEach((row => {
        row.cells.forEach((_ref3 => {
            let {
                pdfCell: pdfCell
            } = _ref3;
            if ((0, _type.isDefined)(pdfCell.text)) {
                const lines = getTextLines(doc, pdfCell.text, pdfCell.font, {
                    wordWrapEnabled: pdfCell.wordWrapEnabled,
                    targetRectWidth: calculateTargetRectWidth(pdfCell._rect.w, pdfCell.padding)
                });
                pdfCell.text = lines.join("\n")
            }
        }))
    }))
}

function applyRtl(doc, rectsByPages, options) {
    rectsByPages.forEach((pageRects => {
        pageRects.forEach((pdfCell => {
            const mirroredX = getPageWidth(doc) - (pdfCell._rect.x + pdfCell._rect.w);
            const marginDiff = options.margin.left - options.margin.right;
            pdfCell._rect.x = mirroredX + marginDiff
        }))
    }))
}
