"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.createProgressBarCell = void 0;

const vrender_1 = require("./../../../vrender"), helper_1 = require("../../../tools/helper"), group_1 = require("../../graphic/group"), get_prop_1 = require("../../utils/get-prop"), padding_1 = require("../../utils/padding"), vutils_1 = require("@visactor/vutils");

function createProgressBarCell(progressBarDefine, style, width, value, dataValue, col, row, padding, table, range) {
    var _a, _b, _c, _d, _e, _f;
    if (progressBarDefine.dependField) {
        const dependField = (0, helper_1.getOrApply)(progressBarDefine.dependField, {
            col: col,
            row: row,
            table: table,
            value: value,
            dataValue: dataValue,
            cellHeaderPaths: void 0
        });
        dataValue = null !== (_b = null === (_a = table.getCellOriginRecord(col, row)) || void 0 === _a ? void 0 : _a[dependField]) && void 0 !== _b ? _b : dataValue;
    }
    const barType = null !== (_c = (0, helper_1.getOrApply)(progressBarDefine.barType, {
        col: col,
        row: row,
        table: table,
        value: value,
        dataValue: dataValue,
        cellHeaderPaths: void 0
    })) && void 0 !== _c ? _c : "default", min = null !== (_d = (0, helper_1.getOrApply)(progressBarDefine.min, {
        col: col,
        row: row,
        table: table,
        value: value,
        dataValue: dataValue,
        cellHeaderPaths: void 0
    })) && void 0 !== _d ? _d : 0, max = null !== (_e = (0, helper_1.getOrApply)(progressBarDefine.max, {
        col: col,
        row: row,
        table: table,
        value: value,
        dataValue: dataValue,
        cellHeaderPaths: void 0
    })) && void 0 !== _e ? _e : min + 100;
    let height = 0;
    if (height = range ? table.getRowsHeight(range.start.row, range.end.row) : table.getRowHeight(row), 
    table.pluginManager) {
        if (table.pluginManager.getPluginByName("Master Detail Plugin")) {
            const bodyRowIndex = row - table.columnHeaderLevelCount, internalProps = table.internalProps, originalHeight = null === (_f = null == internalProps ? void 0 : internalProps.originalRowHeights) || void 0 === _f ? void 0 : _f.get(bodyRowIndex);
            void 0 !== originalHeight && originalHeight > 0 && (height = originalHeight);
        }
    }
    let contentWidth = width, contentHeight = height, _contentOffset = 0;
    (0, vutils_1.isNumber)(table.theme._contentOffset) && (_contentOffset = table.theme._contentOffset);
    const percentCompleteBarGroup = new group_1.Group({
        x: -_contentOffset,
        y: -_contentOffset,
        width: contentWidth,
        height: contentHeight
    });
    percentCompleteBarGroup.name = "progress-bar";
    const {showBar: showBar, barColor: barColor, barBgColor: barBgColor, barPositiveColor: barPositiveColor, barNegativeColor: barNegativeColor, barAxisColor: barAxisColor, barRightToLeft: barRightToLeft, showBarMark: showBarMark, barMarkPositiveColor: barMarkPositiveColor, barMarkNegativeColor: barMarkNegativeColor, barMarkWidth: barMarkWidth, barMarkPosition: barMarkPosition, barMarkInBar: barMarkInBar} = style;
    let {barHeight: barHeight, barBottom: barBottom, barPadding: barPadding} = style;
    1 === barPadding.length ? barPadding = [ barPadding[0], barPadding[0], barPadding[0], barPadding[0] ] : 2 === barPadding.length ? barPadding = [ barPadding[0], barPadding[1], barPadding[0], barPadding[1] ] : 3 === barPadding.length && (barPadding = [ barPadding[0], barPadding[1], barPadding[2], barPadding[1] ]), 
    barPadding = barPadding.map(((v, i) => {
        if ("string" == typeof v && helper_1.str.endsWith(v, "%")) {
            const num = Number(v.substr(0, v.length - 1));
            return 0 === i || 2 === i ? height * num / 100 : width * num / 100;
        }
        return Number(v);
    }));
    const borderWidth = (0, padding_1.getQuadProps)((0, get_prop_1.getProp)("borderLineWidth", style, col, row, table)), barPaddingTop = Math.max(barPadding[0], Math.ceil(borderWidth[0] / 2));
    let barPaddingRight = Math.max(barPadding[1], Math.floor(borderWidth[1] / 2));
    const barPaddingBottom = Math.max(barPadding[2], Math.floor(borderWidth[2] / 2));
    let barPaddingLeft = Math.max(barPadding[3], Math.ceil(borderWidth[3] / 2));
    showBarMark && barMarkWidth > 0 && "right" === barMarkPosition && !1 === barMarkInBar && (barPaddingRight += barMarkWidth, 
    barPaddingLeft += barMarkWidth), contentWidth -= barPaddingRight + barPaddingLeft, 
    contentHeight -= barPaddingBottom + barPaddingTop, row === table.rowCount - 1 && [ 0, "0" ].includes(barBottom) && (contentHeight -= 1);
    const top = barPaddingTop, left = barPaddingLeft, right = contentWidth;
    "string" == typeof barHeight && helper_1.str.endsWith(barHeight, "%") ? (barHeight = Number(barHeight.substr(0, barHeight.length - 1)), 
    barHeight = contentHeight * barHeight / 100) : barHeight = Number(barHeight), "string" == typeof barBottom && helper_1.str.endsWith(barBottom, "%") ? (barBottom = Number(barBottom.substr(0, barBottom.length - 1)), 
    barBottom = contentHeight * barBottom / 100) : barBottom = Number(barBottom);
    if ((0, helper_1.getOrApply)(showBar, {
        col: col,
        row: row,
        table: table,
        context: null,
        value: value,
        dataValue: dataValue
    })) {
        let svalue = `${dataValue}`;
        helper_1.str.endsWith(svalue, "%") && (svalue = svalue.substr(0, svalue.length - 1));
        const num = Number(svalue);
        if (isNaN(num)) return percentCompleteBarGroup;
        if ("default" === (null != barType ? barType : "default")) {
            const percentile = num < min ? 0 : num > max ? 1 : (num - min) / (max - min), barMaxWidth = contentWidth, barTop = top + contentHeight - barHeight - barBottom;
            let barSize = Math.min(barMaxWidth * percentile, barMaxWidth);
            const barLeft = barRightToLeft ? left + right - barSize : left;
            col !== table.colCount - 1 || 1 !== percentile || barRightToLeft || (barSize -= 1);
            const bgFillColor = (0, helper_1.getOrApply)(barBgColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: percentile
            });
            if (bgFillColor) {
                const barBack = (0, vrender_1.createRect)({
                    x: barLeft,
                    y: barTop,
                    width: barMaxWidth,
                    height: barHeight,
                    fill: bgFillColor
                });
                percentCompleteBarGroup.addChild(barBack);
            }
            const fillColor = (0, helper_1.getOrApply)(barColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: percentile
            }) || "#20a8d8", barMain = (0, vrender_1.createRect)({
                x: barLeft,
                y: barTop,
                width: barSize,
                height: barHeight,
                fill: fillColor
            });
            percentCompleteBarGroup.addChild(barMain);
        } else if ("negative" === barType) {
            const negativeRange = min < 0 ? -min : 0, positiveRange = max > 0 ? max : 0, negativeFactor = negativeRange / (negativeRange + positiveRange), positiveFactor = 1 - negativeFactor, positiveRate = num > 0 ? num / positiveRange : 0, negativeRate = num < 0 ? -num / negativeRange : 0, barMaxWidth = contentWidth, barTop = top + contentHeight - barHeight - barBottom, barLeft = left, bgFillColor = (0, 
            helper_1.getOrApply)(barBgColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: positiveRate
            });
            if (bgFillColor) {
                const barBack = (0, vrender_1.createRect)({
                    x: barLeft,
                    y: barTop,
                    width: barMaxWidth,
                    height: barHeight,
                    fill: bgFillColor
                });
                percentCompleteBarGroup.addChild(barBack);
            }
            const positiveLeft = barRightToLeft ? positiveFactor * barMaxWidth : negativeFactor * barMaxWidth, barSizeNega = Math.min(barMaxWidth * negativeFactor * negativeRate, barMaxWidth), barRectNega = barRightToLeft ? {
                left: barLeft + positiveLeft,
                top: barTop,
                width: barSizeNega,
                height: barHeight
            } : {
                left: barLeft + positiveLeft - barSizeNega,
                top: barTop,
                width: barSizeNega,
                height: barHeight
            }, barNagiFillColor = (0, helper_1.getOrApply)(barNegativeColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: negativeRate
            }) || "#20a8d8", barNega = (0, vrender_1.createRect)({
                x: barRectNega.left,
                y: barRectNega.top,
                width: barRectNega.width,
                height: barRectNega.height,
                fill: barNagiFillColor
            });
            percentCompleteBarGroup.addChild(barNega);
            let barSizePosi = Math.min(barMaxWidth * positiveFactor * positiveRate, barMaxWidth);
            col !== table.colCount - 1 || 1 !== positiveRate || barRightToLeft || (barSizePosi -= 1);
            const barRectPosi = barRightToLeft ? {
                left: barLeft + positiveLeft - barSizePosi,
                top: barTop,
                width: barSizePosi,
                height: barHeight
            } : {
                left: barLeft + positiveLeft,
                top: barTop,
                width: barSizePosi,
                height: barHeight
            }, barPosiFillColor = (0, helper_1.getOrApply)(barPositiveColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: positiveRate
            }) || "#20a8d8", barPosi = (0, vrender_1.createRect)({
                x: barRectPosi.left,
                y: barRectPosi.top,
                width: barRectPosi.width,
                height: barRectPosi.height,
                fill: barPosiFillColor
            });
            percentCompleteBarGroup.addChild(barPosi);
            const lineLeft = barRightToLeft ? barRectNega.left : barRectPosi.left, lineStrokeColor = (0, 
            helper_1.getOrApply)(barAxisColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: positiveRate
            }), line = (0, vrender_1.createLine)({
                x: 0,
                y: 0,
                stroke: lineStrokeColor,
                lineWidth: 1,
                lineDash: [ 2, 2 ],
                points: [ {
                    x: lineLeft,
                    y: 0
                }, {
                    x: lineLeft,
                    y: height
                } ]
            });
            if (percentCompleteBarGroup.addChild(line), showBarMark && (positiveRate || negativeRate)) {
                const lineWidth = barMarkWidth, points = [];
                let barMarkStrokeColor;
                if (positiveRate > 0) if (barMarkStrokeColor = (0, helper_1.getOrApply)(barMarkPositiveColor, {
                    col: col,
                    row: row,
                    table: table,
                    context: null,
                    value: value,
                    dataValue: dataValue,
                    percentile: positiveRate
                }) || "#20a8d8", "right" === barMarkPosition) {
                    const markLeft = barRightToLeft ? barRectPosi.left + barMarkWidth / 2 : barRectPosi.left + barRectPosi.width + (barMarkInBar ? -barMarkWidth / 2 : barMarkWidth / 2);
                    points.push({
                        x: markLeft,
                        y: barRectPosi.top
                    }), points.push({
                        x: markLeft,
                        y: barRectPosi.top + barRectPosi.height
                    });
                } else "bottom" === barMarkPosition && (points.push({
                    x: barRectPosi.left,
                    y: barRectPosi.top + barRectPosi.height - barMarkWidth / 2
                }), points.push({
                    x: barRectPosi.left + barRectPosi.width,
                    y: barRectPosi.top + barRectPosi.height - barMarkWidth / 2
                })); else if (negativeRate > 0) if (barMarkStrokeColor = (0, helper_1.getOrApply)(barMarkNegativeColor, {
                    col: col,
                    row: row,
                    table: table,
                    context: null,
                    value: value,
                    dataValue: dataValue,
                    percentile: negativeRate
                }) || "#20a8d8", "right" === barMarkPosition) {
                    const markLeft = barRightToLeft ? barRectNega.left + barRectNega.width - barMarkWidth / 2 : barRectNega.left + barMarkWidth / 2;
                    points.push({
                        x: markLeft,
                        y: barRectNega.top
                    }), points.push({
                        x: markLeft,
                        y: barRectNega.top + barRectNega.height
                    });
                } else "bottom" === barMarkPosition && (points.push({
                    x: barRectNega.left,
                    y: barRectNega.top + barRectNega.height - barMarkWidth / 2
                }), points.push({
                    x: barRectNega.left + barRectNega.width,
                    y: barRectNega.top + barRectNega.height - barMarkWidth / 2
                }));
                const barMark = (0, vrender_1.createLine)({
                    x: 0,
                    y: 0,
                    stroke: barMarkStrokeColor,
                    lineWidth: lineWidth,
                    points: points
                });
                percentCompleteBarGroup.addChild(barMark);
            }
        } else if ("negative_no_axis" === barType) {
            const _negativeRange = min < 0 ? -min : 0, _positiveRange = max > 0 ? max : 0, range = Math.max(_negativeRange, _positiveRange), percentile = 0 === range ? 0 : Math.abs(num) / range, barMaxWidth = contentWidth;
            let barSize = Math.min(barMaxWidth * percentile, barMaxWidth);
            col !== table.colCount - 1 || 1 !== percentile || barRightToLeft || (barSize -= 1);
            const barTop = top + contentHeight - barHeight - barBottom, barLeft = barRightToLeft ? left + right - barSize : left, bgFillColor = (0, 
            helper_1.getOrApply)(barBgColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: percentile
            });
            if (bgFillColor) {
                const barBack = (0, vrender_1.createRect)({
                    x: barLeft,
                    y: barTop,
                    width: barMaxWidth,
                    height: barHeight,
                    fill: bgFillColor
                });
                percentCompleteBarGroup.addChild(barBack);
            }
            const barRect = {
                left: barLeft,
                top: barTop,
                width: barSize,
                height: barHeight
            };
            let barRectFillColor;
            barRectFillColor = num >= 0 ? (0, helper_1.getOrApply)(barPositiveColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                percentile: percentile,
                dataValue: dataValue
            }) || "#20a8d8" : (0, helper_1.getOrApply)(barNegativeColor, {
                col: col,
                row: row,
                table: table,
                context: null,
                value: value,
                dataValue: dataValue,
                percentile: percentile
            }) || "#20a8d8";
            const bar = (0, vrender_1.createRect)({
                x: barRect.left,
                y: barRect.top,
                width: barRect.width,
                height: barRect.height,
                fill: barRectFillColor
            });
            if (percentCompleteBarGroup.addChild(bar), showBarMark && num) {
                const lineWidth = barMarkWidth, points = [];
                let barMarkStrokeColor;
                if (barMarkStrokeColor = num >= 0 ? (0, helper_1.getOrApply)(barMarkPositiveColor, {
                    col: col,
                    row: row,
                    table: table,
                    context: null,
                    value: value,
                    dataValue: dataValue,
                    percentile: percentile
                }) || "#20a8d8" : (0, helper_1.getOrApply)(barMarkNegativeColor, {
                    col: col,
                    row: row,
                    table: table,
                    context: null,
                    value: value,
                    dataValue: dataValue,
                    percentile: percentile
                }) || "#20a8d8", "right" === barMarkPosition) {
                    const markLeft = barRightToLeft ? barRect.left + barMarkWidth / 2 : barRect.left + barRect.width - barMarkWidth / 2;
                    points.push({
                        x: markLeft,
                        y: barRect.top
                    }), points.push({
                        x: markLeft,
                        y: barRect.top + barRect.height
                    });
                } else "bottom" === barMarkPosition && (points.push({
                    x: barRect.left,
                    y: barRect.top + barRect.height - barMarkWidth / 2
                }), points.push({
                    x: barRect.left + barRect.width,
                    y: barRect.top + barRect.height - barMarkWidth / 2
                }));
                const barMark = (0, vrender_1.createLine)({
                    x: 0,
                    y: 0,
                    stroke: barMarkStrokeColor,
                    lineWidth: lineWidth,
                    points: points
                });
                percentCompleteBarGroup.addChild(barMark);
            }
        }
    }
    return percentCompleteBarGroup;
}

exports.createProgressBarCell = createProgressBarCell;
//# sourceMappingURL=progress-bar-cell.js.map
