"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.biConv = exports.expDecs = exports.tickRange = exports.sqrt = exports.encodeSqrtRatioX96 = void 0;
/**
 * @fileoverview Utilities and helpers.
 */
var jsbi_1 = __importDefault(require("jsbi"));
var invariant_1 = __importDefault(require("invariant"));
var MAX_SAFE_INTEGER = jsbi_1.default.BigInt(Number.MAX_SAFE_INTEGER);
var ZERO = jsbi_1.default.BigInt(0);
var ONE = jsbi_1.default.BigInt(1);
var TWO = jsbi_1.default.BigInt(2);
/**
 * Calculates the sqrt ratio as a Q64.96 corresponding to a given ratio of amount1 and amount0
 *
 * @param {bigint} amount1 the numerator amount, i.e. amount of token1.
 * @param {bigint} amount0 the denominator amount, i.en amount of token0.
 * @return {bigint} the sqrt ratio.
 */
var encodeSqrtRatioX96 = function (amount1, amount0) {
    var numerator = jsbi_1.default.leftShift(jsbi_1.default.BigInt(amount1), jsbi_1.default.BigInt(192));
    var denominator = jsbi_1.default.BigInt(amount0);
    var ratioX192 = jsbi_1.default.divide(numerator, denominator);
    return (0, exports.sqrt)(ratioX192);
};
exports.encodeSqrtRatioX96 = encodeSqrtRatioX96;
/**
 * Computes floor(sqrt(value))
 *
 * @param {bigint} value the value for which to compute the square root, rounded down
 * @return {bigint}
 */
var sqrt = function (value) {
    (0, invariant_1.default)(jsbi_1.default.greaterThanOrEqual(value, ZERO), 'NEGATIVE');
    // rely on built in sqrt if possible
    if (jsbi_1.default.lessThan(value, MAX_SAFE_INTEGER)) {
        return jsbi_1.default.BigInt(Math.floor(Math.sqrt(jsbi_1.default.toNumber(value))));
    }
    var z = value;
    var x = jsbi_1.default.add(jsbi_1.default.divide(value, TWO), ONE);
    while (jsbi_1.default.lessThan(x, z)) {
        z = x;
        x = jsbi_1.default.divide(jsbi_1.default.add(jsbi_1.default.divide(value, x), x), TWO);
    }
    return z;
};
exports.sqrt = sqrt;
/**
 * Calculates the tick local range (bottom and top) values given the tick and
 * spacing.
 *
 * @param {string} tickStr The tick value.
 * @param {string} tickSpacingStr The tick spacing value.
 * @param {number=} tickStep How many tick steps wide to capture liquidity.
 * @return {Array<number>} a tuple of the lower and highest tick local range.
 */
var tickRange = function (tickStr, tickSpacingStr, tickStep) {
    if (tickStep === void 0) { tickStep = 0; }
    var tick = Number(tickStr);
    var tickSpacing = Number(tickSpacingStr);
    var tickSpacingStepped = tickSpacing * tickStep;
    var tickLow = Math.floor(tick / tickSpacing) * tickSpacing - tickSpacingStepped;
    var tickHigh = tickLow + tickSpacing + tickSpacingStepped * 2;
    return [tickLow, tickHigh];
};
exports.tickRange = tickRange;
/**
 * Will calculate the exponent for the given decimals number.
 *
 * @param {string|number} decs The decimal number.
 * @return {bigint} The decimals exponent.
 */
var expDecs = function (decs) {
    return jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(decs));
};
exports.expDecs = expDecs;
/**
 * Converts a value to JSBI, if it's already a JSBI will just return it.
 *
 * @param {string|number|JSBI} numstr The value to convert.
 * @return {bigint} JSBI representation of the value.
 */
var biConv = function (numstr) {
    return typeof numstr !== 'bigint' ? jsbi_1.default.BigInt(numstr) : numstr;
};
exports.biConv = biConv;
//# sourceMappingURL=utils.js.map