/**
 * DevExtreme (esm/exporter/jspdf/draw_pdf_table.js)
 * Version: 21.1.7 (build 22012-0313)
 * Build date: Wed Jan 12 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    extend
} from "../../core/utils/extend";
import {
    isDefined
} from "../../core/utils/type";
import {
    drawLine,
    drawRect,
    drawTextInRect
} from "./pdf_utils";
export function drawPdfTable(doc, styles, table, options) {
    if (!isDefined(doc)) {
        throw "doc is required"
    }
    var {
        allowDrawBorders: allowDrawBorders,
        allowDrawCustomBorders: allowDrawCustomBorders,
        allowDrawCellContent: allowDrawCellContent
    } = null !== options && void 0 !== options ? options : {};

    function drawBorder(rect) {
        var drawLeftBorder = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : true;
        var drawRightBorder = arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : true;
        var drawTopBorder = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : true;
        var drawBottomBorder = arguments.length > 4 && void 0 !== arguments[4] ? arguments[4] : true;
        if (!isDefined(rect)) {
            throw "rect is required"
        }
        var defaultBorderLineWidth = 1;
        if (!drawLeftBorder && !drawRightBorder && !drawTopBorder && !drawBottomBorder) {
            return
        } else if (drawLeftBorder && drawRightBorder && drawTopBorder && drawBottomBorder) {
            doc.setLineWidth(defaultBorderLineWidth);
            drawRect(doc, rect.x, rect.y, rect.w, rect.h)
        } else {
            doc.setLineWidth(defaultBorderLineWidth);
            if (drawTopBorder) {
                drawLine(doc, rect.x, rect.y, rect.x + rect.w, rect.y)
            }
            if (drawLeftBorder) {
                drawLine(doc, rect.x, rect.y, rect.x, rect.y + rect.h)
            }
            if (drawRightBorder) {
                drawLine(doc, rect.x + rect.w, rect.y, rect.x + rect.w, rect.y + rect.h)
            }
            if (drawBottomBorder) {
                drawLine(doc, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h)
            }
        }
    }

    function drawRow(rowCells) {
        if (!isDefined(rowCells)) {
            throw "rowCells is required"
        }
        rowCells.forEach(cell => {
            if (true === cell.skip) {
                return
            }
            if (!isDefined(cell._rect)) {
                throw "cell._rect is required"
            }
            if (true === allowDrawCellContent) {
                ! function(doc, cell) {
                    if (isDefined(cell.backgroundColor)) {
                        doc.setFillColor(cell.backgroundColor);
                        drawRect(doc, cell._rect.x, cell._rect.y, cell._rect.w, cell._rect.h, "F")
                    }
                }(doc, cell);
                var font = isDefined(cell.font) ? extend({}, styles.font, cell.font) : styles.font;
                var docFont = doc.getFont();
                if (font.name !== docFont.fontName || font.style !== docFont.fontStyle || isDefined(font.weight)) {
                    doc.setFont(font.name, font.style, font.weight)
                }
                if (font.size !== doc.getFontSize()) {
                    doc.setFontSize(font.size)
                }
                var textColor = isDefined(cell.textColor) ? cell.textColor : styles.textColor;
                if (textColor !== doc.getTextColor()) {
                    doc.setTextColor(textColor)
                }
                if (isDefined(cell.text) && "" !== cell.text) {
                    drawTextInRect(doc, cell.text, cell._rect, cell.wordWrapEnabled, cell.jsPdfTextOptions)
                }
            }
            var isDrawBorders = isDefined(cell.borderColor) ? true === allowDrawCustomBorders : true === allowDrawBorders;
            if (isDrawBorders) {
                var borderColor = isDefined(cell.borderColor) ? cell.borderColor : styles.borderColor;
                if (borderColor !== doc.getDrawColor()) {
                    doc.setDrawColor(borderColor)
                }
                drawBorder(cell._rect, cell.drawLeftBorder, cell.drawRightBorder, cell.drawTopBorder, cell.drawBottomBorder)
            }
        })
    }
    if (!isDefined(table)) {
        return Promise.resolve()
    }
    if (!isDefined(table.rect)) {
        throw "table.rect is required"
    }
    if (isDefined(table.rows)) {
        for (var rowIndex = 0; rowIndex < table.rows.length; rowIndex++) {
            drawRow(table.rows[rowIndex])
        }
    }
    if (true === allowDrawBorders && (isDefined(table.drawTableBorder) ? table.drawTableBorder : isDefined(table.rows) && 0 === table.rows.length)) {
        drawBorder(table.rect)
    }
}
