"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.positiveDifference = exports.formatTokenUnits = exports.parseTokenUnits = void 0;
const parseTokenUnits = (amount, decimals) => {
    const normalized = amount.trim();
    if (!/^\d+(\.\d+)?$/.test(normalized)) {
        throw new Error(`Invalid token amount: ${amount}`);
    }
    const [whole, fraction = ""] = normalized.split(".");
    const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
    return (BigInt(whole) * 10n ** BigInt(decimals) + BigInt(paddedFraction || "0")).toString();
};
exports.parseTokenUnits = parseTokenUnits;
const formatTokenUnits = (amount, decimals) => {
    const value = typeof amount === "bigint" ? amount : BigInt(amount);
    const base = 10n ** BigInt(decimals);
    const whole = value / base;
    const fraction = value % base;
    const fractionText = fraction.toString().padStart(decimals, "0").replace(/0+$/, "");
    return fractionText ? `${whole}.${fractionText}` : whole.toString();
};
exports.formatTokenUnits = formatTokenUnits;
const positiveDifference = (current, starting) => {
    const diff = BigInt(current) - BigInt(starting);
    return diff > 0n ? diff : 0n;
};
exports.positiveDifference = positiveDifference;
