"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.avgTotalsPercentageFormatter = void 0;
const utils_1 = require("@slickgrid-universal/utils");
const utilities_1 = require("../services/utilities");
const formatterUtilities_1 = require("../formatters/formatterUtilities");
const avgTotalsPercentageFormatter = (totals, columnDef, grid) => {
    const field = columnDef.field ?? '';
    let val = totals.avg?.[field];
    const params = columnDef?.params;
    let prefix = params?.groupFormatterPrefix || '';
    const suffix = params?.groupFormatterSuffix || '';
    const { minDecimal, maxDecimal, decimalSeparator, thousandSeparator, wrapNegativeNumber } = (0, formatterUtilities_1.retrieveFormatterOptions)(columnDef, grid, 'percent', 'group');
    if ((0, utils_1.isNumber)(val)) {
        if (val < 0) {
            val = Math.abs(val);
            if (!wrapNegativeNumber) {
                prefix += '-';
            }
            else {
                if (isNaN(minDecimal) && isNaN(maxDecimal)) {
                    const outputVal = (0, utilities_1.thousandSeparatorFormatted)(Math.round(val), thousandSeparator);
                    return `${prefix}(${outputVal}%)${suffix}`;
                }
                return `${prefix}(${(0, utilities_1.decimalFormatted)(val, minDecimal, maxDecimal, decimalSeparator, thousandSeparator)}%)${suffix}`;
            }
        }
        if (isNaN(minDecimal) && isNaN(maxDecimal)) {
            const outputVal = (0, utilities_1.thousandSeparatorFormatted)(Math.round(val), thousandSeparator);
            return `${prefix}${outputVal}%${suffix}`;
        }
        return `${prefix}${(0, utilities_1.decimalFormatted)(val, minDecimal, maxDecimal, decimalSeparator, thousandSeparator)}%${suffix}`;
    }
    return '';
};
exports.avgTotalsPercentageFormatter = avgTotalsPercentageFormatter;
//# sourceMappingURL=avgTotalsPercentageFormatter.js.map