"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._checkformat = exports.toAuto = exports.toFixed = exports.toSignificant = void 0;
var invariant_1 = __importDefault(require("invariant"));
var decimal_js_1 = __importDefault(require("decimal.js"));
/**
 * Will convert the fraction of the two tokens into a significant representation.
 *
 * @param {Array<bigint>} fraction Fraction tupple array containing the numerator
 *    and denominator.
 * @param {Object=} optOptions Calculation options.
 * @param {number=} optOptions.significantDigits How many significant digits to use.
 * @param {boolean|Array=} optOptions.format Format the output using Intl.NumberFormat.
 * @param {boolean=} optOptions.reverse Set to true to reverse the ratio calculation.
 * @param {number=} optOptions.rounding Decimal.js rounding constant.
 * @return {string} The result.
 */
var toSignificant = function (fraction, optOptions) {
    if (optOptions === void 0) { optOptions = {}; }
    var format = optOptions.format, reverse = optOptions.reverse;
    var significantDigits = optOptions.decimalPlaces, rounding = optOptions.rounding;
    if (!significantDigits) {
        significantDigits = 5;
    }
    if (!rounding) {
        rounding = decimal_js_1.default.ROUND_HALF_UP;
    }
    var _a = __read(fraction, 2), numerator = _a[0], denominator = _a[1];
    if (reverse) {
        var tmpNumerator = numerator;
        numerator = denominator;
        denominator = tmpNumerator;
    }
    var res = new decimal_js_1.default(numerator.toString())
        .div(denominator.toString())
        .toSignificantDigits(significantDigits, rounding)
        .toString();
    return (0, exports._checkformat)(res, 'significant', significantDigits, format);
};
exports.toSignificant = toSignificant;
/**
 * Will convert the fraction of the two tokens into a fixed representation.
 *
 * @param {Array<bigint>} fraction Fraction tupple Array containing the numerator
 *    and denominator.
 * @param {Object=} optOptions Calculation options.
 * @param {number=} optOptions.decimalPlaces How many decimals to use.
 * @param {boolean|Array=} optOptions.format Format the output using Intl.NumberFormat.
 * @param {boolean=} optOptions.reverse Set to true to reverse the ratio calculation.
 * @param {number=} optOptions.rounding Decimal.js rounding constant.
 * @return {string} The result.
 */
var toFixed = function (fraction, optOptions) {
    if (optOptions === void 0) { optOptions = {}; }
    var format = optOptions.format, reverse = optOptions.reverse;
    var decimalPlaces = optOptions.decimalPlaces, rounding = optOptions.rounding;
    if (!decimalPlaces) {
        decimalPlaces = 5;
    }
    if (!rounding) {
        rounding = decimal_js_1.default.ROUND_HALF_UP;
    }
    var _a = __read(fraction, 2), numerator = _a[0], denominator = _a[1];
    if (reverse) {
        var tmpNumerator = numerator;
        numerator = denominator;
        denominator = tmpNumerator;
    }
    var res = new decimal_js_1.default(numerator.toString())
        .div(denominator.toString())
        .toFixed(decimalPlaces, rounding);
    return (0, exports._checkformat)(res, 'fixed', decimalPlaces, format);
};
exports.toFixed = toFixed;
/**
 * Will divide and format the fraction by either using toFixed or toSignificant
 * automatically, depending if the fracrtion division is above or bellow 1.
 *
 * @param {Array<bigint>} fraction Fraction tupple Array containing the numerator
 *    and denominator.
 * @param {Object=} optOptions Calculation options.
 * @param {number=} optOptions.decimalPlaces How many decimals to use.
 * @param {boolean|Array=} optOptions.format Format the output using Intl.NumberFormat.
 * @param {boolean=} optOptions.reverse Set to true to reverse the ratio calculation.
 * @param {number=} optOptions.rounding Decimal.js rounding constant.
 * @return {string} The result.
 */
var toAuto = function (fraction, optOptions) {
    if (optOptions === void 0) { optOptions = {}; }
    var reverse = optOptions.reverse;
    var _a = __read(fraction, 2), numerator = _a[0], denominator = _a[1];
    if (reverse) {
        var tmpNumerator = numerator;
        numerator = denominator;
        denominator = tmpNumerator;
    }
    var tempRes = decimal_js_1.default.div(numerator.toString(), denominator.toString()).toNumber();
    if (tempRes > 1) {
        return (0, exports.toFixed)(fraction, optOptions);
    }
    return (0, exports.toSignificant)(fraction, optOptions);
};
exports.toAuto = toAuto;
/**
 * Checks and applies format if it exists.
 *
 * @param {string} res result from the calculations
 * @param {string} callee Invoking function.
 * @param {number} decimalPlaces How many decimal places to use.
 * @param {boolean|Array=} optFormat Format the output using Intl.NumberFormat.
 * @return {string} Formatted outcome.
 * @private
 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
 */
var _checkformat = function (res, callee, decimalPlaces, optFormat) {
    if (!optFormat) {
        return res;
    }
    if (optFormat === true) {
        var options = {};
        if (callee === 'significant') {
            options.maximumSignificantDigits = decimalPlaces;
        }
        if (callee === 'fixed') {
            options.maximumFractionDigits = decimalPlaces;
        }
        return Intl.NumberFormat('en-US', options).format(res);
    }
    if (Array.isArray(optFormat)) {
        return Intl.NumberFormat.apply(null, optFormat).format(res);
    }
    (0, invariant_1.default)(false, 'format argument can be either a boolean or an Array');
};
exports._checkformat = _checkformat;
//# sourceMappingURL=fractions.js.map