"use strict";
/**
 * @fileoverview Calculates the price (ratio) from sqrt ratio.
 */
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.sqrtPrice = void 0;
var fractions_1 = require("./fractions");
var jsbi_1 = __importDefault(require("jsbi"));
var constants_1 = require("./constants");
var utils_1 = require("./utils");
function chainContext(fraction) {
    return {
        toFixed: fractions_1.toFixed.bind(null, fraction),
        toSignificant: fractions_1.toSignificant.bind(null, fraction),
        toAuto: fractions_1.toAuto.bind(null, fraction),
        toFraction: function () { return fraction; },
    };
}
/**
 * Calculates the price (ratio) from sqrt price.
 *
 * @param {Array<number|string>} tokenDecimals Array tuple of token decimals.
 * @param {string} sqrtRatioX96 The tick value.
 * @return {Object} The chain context.
 */
var sqrtPrice = function (tokenDecimals, sqrtRatioX96) {
    var _a = __read(tokenDecimals, 2), token0Decimals = _a[0], token1Decimals = _a[1];
    var scalarNumerator = (0, utils_1.expDecs)(token0Decimals);
    var scalarDenominator = (0, utils_1.expDecs)(token1Decimals);
    var sqrtRatioX96BI = jsbi_1.default.BigInt(sqrtRatioX96);
    var inputNumerator = jsbi_1.default.multiply(sqrtRatioX96BI, sqrtRatioX96BI);
    var inputDenominator = constants_1.Q192;
    var adjustedForDecimalsDenominator = jsbi_1.default.BigInt(jsbi_1.default.multiply(scalarDenominator, inputDenominator));
    var adjustedForDecimalsNumerator = jsbi_1.default.BigInt(jsbi_1.default.multiply(scalarNumerator, inputNumerator));
    var numerator = adjustedForDecimalsNumerator;
    var denominator = adjustedForDecimalsDenominator;
    var fraction = [numerator, denominator];
    var chainCtx = chainContext(fraction);
    return chainCtx;
};
exports.sqrtPrice = sqrtPrice;
//# sourceMappingURL=sqrtPrice.js.map