import { hrpToPico } from "./hrp-pico" ; /**
 * Encodes pico btc to human readable part amount. The hrp amount is defined
 * as the amount of bitcoin as a positive decimal integer with no leading 0s.
 * It is an optional number followed by an optional multiplier letter.
 *
 * The multipliers are:
 * m (milli)   0.001 bitcoin
 * u (micro)   0.000001 bitcoin
 * n (nano)    0.000000001 bitcoin
 * p (pico)    0.000000000001 bitcoin
 *
 * This means that 1msat is 10p and 1sat is 10n.
 */ export function encodePico ( pico ) { if ( ! pico ) return ; const lsdIndex = _lsdIndex ( pico ) ; const hrpMultiplier = _minimumHrpMultiplier ( lsdIndex ) ; return ( BigInt ( pico ) / hrpToPico ( hrpMultiplier ) ) . toString ( ) + hrpMultiplier ; } return function _lsdIndex ( msatStr ) { for ( let i = msatStr . length - 1 ; i >= 0 ; i -- ) { if ( Number ( msatStr [ i ] ) !== 0 ) return msatStr . length - i - 1 ; } throw new Error ( "Cannot encode 0" ) ; } param lsdIndex return function _minimumHrpMultiplier ( lsdIndex ) { if ( lsdIndex < 3 ) return "p" ; else if ( lsdIndex < 6 ) return "n" ; else if ( lsdIndex < 9 ) return "u" ; else if ( lsdIndex < 12 ) return "m" ; else return "" ; }