/**
 * Formatting Utilities
 */
/**
 * Format a bigint amount with decimals to a human-readable string.
 *
 * @param amount - The raw amount as bigint (must be non-negative)
 * @param decimals - Number of decimal places (e.g., 18 for ETH)
 * @returns Formatted string (e.g., "1.5" for 1500000000000000000n with 18 decimals)
 *
 * @example
 * ```typescript
 * formatAmount(1500000000000000000n, 18); // "1.5"
 * formatAmount(0n, 18);                   // "0"
 * formatAmount(1n, 18);                   // "0.000000000000000001"
 * ```
 */
export function formatAmount(amount, decimals) {
    if (amount < 0n) {
        throw new Error('formatAmount does not support negative amounts');
    }
    if (amount === 0n)
        return '0';
    const divisor = BigInt(10 ** decimals);
    const whole = amount / divisor;
    const remainder = amount % divisor;
    if (remainder === 0n) {
        return whole.toString();
    }
    const decimalStr = remainder.toString().padStart(decimals, '0');
    const trimmed = decimalStr.replace(/0+$/, '');
    return `${whole}.${trimmed}`;
}
/**
 * Parse a human-readable amount string to bigint.
 *
 * Fractional digits beyond `decimals` are silently truncated (not rounded).
 *
 * @param amount - The amount as string (e.g., "1.5"). Must be a valid decimal number.
 * @param decimals - Number of decimal places (e.g., 18 for ETH)
 * @returns The raw amount as bigint
 * @throws If `amount` is empty or not a valid decimal string
 *
 * @example
 * ```typescript
 * parseAmount("1.5", 18);   // 1500000000000000000n
 * parseAmount("100", 6);    // 100000000n
 * ```
 */
export function parseAmount(amount, decimals) {
    if (!amount || !amount.trim()) {
        throw new Error('Amount string is required');
    }
    const trimmed = amount.trim();
    // Validate format: optional leading minus is not supported for token amounts
    if (!/^\d+(\.\d+)?$/.test(trimmed)) {
        throw new Error(`Invalid amount format: "${amount}". Expected a decimal number (e.g., "1.5")`);
    }
    const [whole, fraction = ''] = trimmed.split('.');
    const paddedFraction = fraction.padEnd(decimals, '0').slice(0, decimals);
    return BigInt(whole + paddedFraction);
}
//# sourceMappingURL=format.js.map