"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLmsrSatsFixed = exports.getLmsrSats = exports.getProbability = exports.lmsrFixedOld = exports.lmsrFixed = exports.lmsr = exports.SatScaling = void 0;
const fixMath_1 = require("./fixMath");
const fixMathOld_1 = require("./fixMathOld");
const lte_1 = __importDefault(require("semver/functions/lte"));
exports.SatScaling = 2 ** 20;
const SatScalingAdjust = 44; // 64 - 20
function lmsr(balance) {
    if (balance.liquidity === 0)
        return Math.max(...balance.shares);
    const expSum = balance.shares
        .map(share => share / balance.liquidity)
        .map(Math.exp)
        .reduce((a, b) => a + b);
    return balance.liquidity * Math.log(expSum);
}
exports.lmsr = lmsr;
function lmsrFixed(balance) {
    if (balance.liquidity === 0)
        return BigInt(Math.max(...balance.shares)) * fixMath_1.bigIntScale;
    const expSum = balance.shares
        .map(share => (BigInt(share) * fixMath_1.bigIntScale) / BigInt(balance.liquidity))
        .map(fixMath_1.exp)
        .reduce((a, b) => a + b);
    return BigInt(balance.liquidity) * (0, fixMath_1.log)(expSum);
}
exports.lmsrFixed = lmsrFixed;
function lmsrFixedOld(balance) {
    if (balance.liquidity === 0)
        return Math.max(...balance.shares);
    const expSum = balance.shares
        .map(share => (share * fixMathOld_1.scale) / balance.liquidity)
        .map(Math.floor)
        .map(fixMathOld_1.exp)
        .reduce((a, b) => a + b);
    return (balance.liquidity * (0, fixMathOld_1.log)(expSum)) / fixMathOld_1.scale;
}
exports.lmsrFixedOld = lmsrFixedOld;
function getProbability(balance, shares) {
    if (balance.liquidity === 0) {
        return shares / balance.shares.reduce((a, b) => a + b);
    }
    const expSum = balance.shares
        .map(share => share / balance.liquidity)
        .map(Math.exp)
        .reduce((a, b) => a + b);
    return Math.exp(shares / balance.liquidity) / expSum;
}
exports.getProbability = getProbability;
function getLmsrSats(balance) {
    return Math.floor(lmsr(balance) * exports.SatScaling);
}
exports.getLmsrSats = getLmsrSats;
function getLmsrSatsFixed(balance, version) {
    if ((0, lte_1.default)(version.version, "0.3.13")) {
        return Math.floor(lmsrFixedOld(balance) * exports.SatScaling);
    }
    return Number(lmsrFixed(balance) >> BigInt(SatScalingAdjust));
}
exports.getLmsrSatsFixed = getLmsrSatsFixed;
//# sourceMappingURL=lmsr.js.map