"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateMaxPrincipalPerCollateralRatio = exports.calculateBorrowerPayoutFromLoanPrincipal = exports.calculatePrincipalRequiredForBorrowerPayout = void 0;
const FACTOR = 10000n;
function calculatePrincipalRequiredForBorrowerPayout(expectedBorrowerPayment, // 5000   ///amount of wei THE BORROWER must get out of this loan
marketFeePct, // 500
protocolFeePct // 5
) {
    const totalFeesPct = BigInt(marketFeePct) + BigInt(protocolFeePct);
    const principalRequired = (BigInt(expectedBorrowerPayment) * FACTOR) / (FACTOR - totalFeesPct); // rounding UP
    // ---- If we are off by 1,  subtract 1
    const calculatedBorrowerPayment = calculateBorrowerPayoutFromLoanPrincipal(principalRequired, marketFeePct, protocolFeePct);
    const offByError = calculatedBorrowerPayment - BigInt(expectedBorrowerPayment);
    return principalRequired - offByError;
}
exports.calculatePrincipalRequiredForBorrowerPayout = calculatePrincipalRequiredForBorrowerPayout;
// this is how it happens in solidity
function calculateBorrowerPayoutFromLoanPrincipal(loanPrincipal, marketFeePct, protocolFeePct) {
    const marketFee = (BigInt(loanPrincipal) * BigInt(marketFeePct)) / FACTOR;
    const protocolFee = (BigInt(loanPrincipal) * BigInt(protocolFeePct)) / FACTOR;
    const borrowerPayout = BigInt(loanPrincipal) - marketFee - protocolFee;
    return borrowerPayout;
}
exports.calculateBorrowerPayoutFromLoanPrincipal = calculateBorrowerPayoutFromLoanPrincipal;
/*
  Returns the _maxPrincipalPerCollateralRatio raw value to be used for LenderCommitments

*/
function calculateMaxPrincipalPerCollateralRatio(maxLoanPrincipal, // base units of the principal token
collateralRequiredForMaxLoan, // base units of the collateral token
principalTokenDecimals, collateralTokenDecimals) {
    if (isNaN(principalTokenDecimals))
        throw new Error('principalTokenDecimals must be a number');
    if (isNaN(collateralTokenDecimals))
        throw new Error('collateralTokenDecimals must be a number');
    const totalTokenDecimals = BigInt(principalTokenDecimals) + BigInt(collateralTokenDecimals);
    const expandedMaxLoanPrincipal = BigInt(maxLoanPrincipal) * 10n ** totalTokenDecimals;
    return expandedMaxLoanPrincipal / BigInt(collateralRequiredForMaxLoan);
}
exports.calculateMaxPrincipalPerCollateralRatio = calculateMaxPrincipalPerCollateralRatio;
//# sourceMappingURL=teller-math.js.map