import { i as isWiseQuote, a as isPartnerQuote, b as isBankQuote } from './quoteFilters-9ddc9e2f.js';
import './provider-constants-b35398f2.js';

/* eslint-disable fp/no-mutation */
function extractWiseQuote(quotes) {
  return quotes.find(q => isWiseQuote(q));
}
function hasWiseQuote(quotes) {
  return !!extractWiseQuote(quotes);
}
function hasNonWiseQuote(quotes, excludePartners) {
  if (excludePartners) {
    return !!quotes.some(quote => !isWiseQuote(quote) && !isPartnerQuote(quote));
  }
  return !!quotes.some(quote => !isWiseQuote(quote));
}
function extractCheapestNonWiseQuote(quotes, excludePartners) {
  let nonTwQuotes;
  if (excludePartners) {
    nonTwQuotes = quotes.filter(quote => !isWiseQuote(quote) && !isPartnerQuote(quote));
  } else {
    nonTwQuotes = quotes.filter(quote => !isWiseQuote(quote));
  }
  return nonTwQuotes.reduce((previous, current) => previous.receivedAmount >= current.receivedAmount ? previous : current, nonTwQuotes[0]);
}
function calculateSavingAgainstCheapestNonWiseQuoteInTargetCurrency(quotes, excludePartners) {
  const twQuote = extractWiseQuote(quotes);
  const bestNonTwQuote = extractCheapestNonWiseQuote(quotes, excludePartners);
  return twQuote.receivedAmount - bestNonTwQuote.receivedAmount;
}
function supportsCalculatingSavingsAgainstWiseQuote(quotes = [], excludePartners) {
  return quotes.length >= 2 && hasWiseQuote(quotes) && hasNonWiseQuote(quotes, excludePartners);
}
function isNonPartnerBankQuote(quote) {
  return isBankQuote(quote) && !isWiseQuote(quote) && !isPartnerQuote(quote);
}
function extractMostExpensiveBankQuote(quotes) {
  const bankQuotes = quotes.filter(isNonPartnerBankQuote);
  return bankQuotes.reduce((previous, current) => previous.receivedAmount <= current.receivedAmount ? previous : current, bankQuotes[0]);
}
function extractCheapestQuote(quotes, excludePartners) {
  let providerQuotes;
  if (excludePartners) {
    providerQuotes = quotes.filter(quote => !isPartnerQuote(quote));
  } else {
    providerQuotes = quotes;
  }
  return providerQuotes.reduce((previous, current) => previous.receivedAmount >= current.receivedAmount ? previous : current, quotes[0]);
}
function extractMostExpensiveQuote(quotes, excludePartners) {
  let providerQuotes;
  if (excludePartners) {
    providerQuotes = quotes.filter(quote => !isPartnerQuote(quote));
  } else {
    providerQuotes = quotes;
  }
  return providerQuotes.reduce((previous, current) => previous.receivedAmount <= current.receivedAmount ? previous : current, quotes[0]);
}
function hasNonPartnerBankQuote(quotes) {
  return !!quotes.some(isNonPartnerBankQuote);
}
function supportsCalculatingSavingAgainstMostExpensiveBank(quotes = []) {
  return quotes.length >= 2 && hasWiseQuote(quotes) && hasNonPartnerBankQuote(quotes);
}
function calculateSavingAgainstMostExpensiveBankInTargetCurrency(quotes = []) {
  const twQuote = extractWiseQuote(quotes);
  const mostExpensiveBankQuote = extractMostExpensiveBankQuote(quotes);
  return twQuote.receivedAmount - mostExpensiveBankQuote.receivedAmount;
}
function calculateSavingAgainstMostExpensiveBankInSourceCurrency(quotes = []) {
  const savingInTargetCurrency = calculateSavingAgainstMostExpensiveBankInTargetCurrency(quotes);
  const twQuote = extractWiseQuote(quotes);
  return savingInTargetCurrency / twQuote.rate;
}
function calculateSavingsWithRespectToWiseQuote(quotes, excludePartners) {
  if (!supportsCalculatingSavingsAgainstWiseQuote(quotes, excludePartners)) {
    return;
  }
  const cheapestQuote = extractCheapestQuote(quotes, excludePartners);
  let savings;
  if (isWiseQuote(cheapestQuote)) {
    const mostExpensiveQuote = extractMostExpensiveQuote(quotes, excludePartners);
    savings = cheapestQuote.receivedAmount - mostExpensiveQuote.receivedAmount;
  } else {
    const wiseQuote = extractWiseQuote(quotes);
    savings = wiseQuote.receivedAmount - cheapestQuote.receivedAmount;
  }
  return Math.round(savings * 100) / 100;
}

export { calculateSavingAgainstCheapestNonWiseQuoteInTargetCurrency, calculateSavingAgainstMostExpensiveBankInSourceCurrency, calculateSavingAgainstMostExpensiveBankInTargetCurrency, calculateSavingsWithRespectToWiseQuote, supportsCalculatingSavingAgainstMostExpensiveBank, supportsCalculatingSavingsAgainstWiseQuote };
//# sourceMappingURL=savingsCalculator.js.map
