import { Money } from "bigint-money/dist" ; import { NumberFormatter } from "../NumberFormatter" ; import { Fee } from "./Fee" ; export class CappedFee implements Fee { constructor ( private minimum , private maximum , private fee ) { } public calculateFor ( amount ) { const fee = this . fee . calculateFor ( amount ) ; if ( this . minimum . isGreaterThan ( fee ) ) { return this . minimum ; } if ( this . maximum && this . maximum . isLesserThan ( fee ) ) { return this . maximum ; } return fee ; } public describe ( ) { const numberFormatter = new NumberFormatter ( ) ; if ( ! this . maximum ) { return this . fee . describe ( ) + ' (min. ' + numberFormatter . formatMoney ( this . minimum ) + ')' ; } if ( this . minimum . isEqual ( 0 ) ) { return this . fee . describe ( ) + ' (max. ' + numberFormatter . formatMoney ( this . maximum ) + ')' ; } return this . fee . describe ( ) + ' (min. ' + numberFormatter . formatMoney ( this . minimum ) + ', max. ' + numberFormatter . formatMoney ( this . maximum ) + ')' ; } public getExtendedDescription ( ) { const numberFormatter = new NumberFormatter ( ) ; let parts = [ ] ; if ( this . minimum . isGreaterThan ( 0 ) ) { parts . push ( 'Minimum ' + numberFormatter . formatMoney ( this . minimum ) ) ; } if ( this . maximum ) { parts . push ( 'Maximum: ' + numberFormatter . formatMoney ( this . maximum ) ) ; } return this . fee . getExtendedDescription ( ) . concat ( parts ) ; } }